Sort Header

Posted by Abel Gancsos on Dec 19, 2011 in Blog |

Here is a compiled header file for the most used sorting algorithms. Feel free to edit it to your needs:

#include
#include
using namespace std;

void quickSort(int a[],int left,int right);
void restoreup(int a[],int i);
void restoredown(int a[],int i,int n);
char *mergeSort(char letters[], char temp[], int array_size);
char *mSort(char letters[], char temp[], int left, int right);
char *merge(char letters[], char temp[], int left, int mid, int right);
void charBubbleSort(char a[],int max);
void bubbleSort(int a[],int max);

void restoreup(int a[],int i)
{
int value=a[i];

while((i>1)&&(a[i/2]{
a[i]=a[i/2];
i=i/2;
}

a[i]=value;
}

void restoredown(int a[],int i,int n)
{
int value=a[i],j=i*2;

while(j{
if((j j++;

if(a[j]break;

a[j/2]=a[j];
j=j*2;
}

a[j/2]=value;
}

void quickSort(int a[],int left,int right){
int pivot=a[(left+right)/2],tmp;
int i=left,j=right;

while(iwhile(a[i] i++;
}
while(a[j]>pivot){
j–;
}

if(itmp=a[i];
a[i]=a[i+1];
a[i+1]=tmp;
i++;
j–;
}
}

if(leftquickSort(a,left,j);

if(iquickSort(a,i,right);

//return a;
}

char *mergeSort(char letters[], char temp[], int array_size)
{
return mSort( letters, temp, 0, array_size – 1);
}

char *mSort(char letters[], char temp[], int left, int right)
{
int mid;

if (right > left)
{
mid = (right + left) / 2;
mSort( letters, temp, left, mid);
mSort( letters, temp, (mid+1), right);

merge( letters, temp, left, (mid+1), right);
}

return letters;
}

char *merge(char letters[], char temp[], int left, int mid, int right)
{
int i, leftEnd, elements, tmpPos;

leftEnd = (mid – 1);
tmpPos = left;
elements = (right – left + 1);

while ((left {
if ( letters[left] {
temp[tmpPos] = letters[left];
tmpPos += 1;
left += 1;
}
else
{
temp[tmpPos] = letters[mid];
tmpPos += 1;
mid += 1;
}
}

while (left {
temp[tmpPos] = letters[left];
left += 1;
tmpPos += 1;
}
while (mid {
temp[tmpPos] = letters[mid];
mid += 1;
tmpPos += 1;
}

for (i=0; i < elements; i++)
{
letters[right] = temp[right];
right -= 1;
}

return temp;
}

void bubbleSort(int a[],int max){

for(int i=0;ifor(int j=0;jif(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

void charBubbleSort(char a[],int max){

for(int i=0;ifor(int j=0;jif(a[j]>a[j+1]){
char temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

You can download it from http://foxwebhosting.dyndns.org/lioncage/Packages/sort.h

Comments are closed.

Copyright © 2012 Abe's the Word All rights reserved. Theme by Laptop Geek.