Summary of 8 Classic sorting algorithms

Source: Internet
Author: User

1. Two points search
Code: int binarysearch (int arr[],int l,int r,int x)
{
while (L <= R)
{
int m = l + (r-1)/2;//in order to prevent (l+r overflow)
if (arr[m] = = x)
return m;
if (Arr[m] < x)
L = m + 1;
Else
R = m-1;
}
return-1;
}
Problem Extension 1: Given an ordered array, find the given element with a minimum number of comparisons. Code: int binarysearch (int arr[],int l,int r,int x)//r should be initialized to Arr.length
{
int m;
while (R-l > L)
{
m = L + (r-l)/2;
if (Arr[m] <= x)
L = m;
Else
r = m;
}
if (arr[l] = = x)
return l;
Else
return-1;
}
Problem Extension 2:Given An array of N distinct integers, find floor value of input ' key '. Say, A = {-1, 2, 3, 5, 6, 8, 9, ten} and key = 7, we should return 6 as outcome.Code:int binarysearch (int arr[],int l,int r,int x){   int m; while (R-l > L)
{
m = L + (r-l)/2;
if (Arr[m] <= x)
L = m;
Else
r = m;
}
return arr[l];
}
int floor (int arr[],int size,int x){if (x < arr[0])return-1;return BinarySearch (arr,0, size, x);//Note that R is initialized to size}
problem Extension 3:Given a sorted array with possible duplicate elements. Find number of occurrences of input ' key ' inLog NTime .Code:int getrightposition (int a[],int l,int r,int key){int m;While (R-l > 1)     {m = l + (r-l)/2;if (A[m] <= key)L = m;Elser = m;     }return 1;}
int getleftposition (int a[],int l,int r,int key){int m;While (R-l > 1)     {m = l + (r-l)/2;if (A[m] >= key)r = m;ElseL = m;     }return R;}
int countoccurances (int a[],int size,int key){int left = getleftposition (A,-1,size-1,key);int rigth = getrightposition (A,0,size,key);if (a[left] = = Key && key = = A[right])return right-right+1;Elsereturn 0;}

2. Select Sort
Code implementation: void Selectionsort (int arr[],int N)
{
int i,j,min_index;
for (i = 0;i < n;i++)
{
Min_index = i;
for (j = i;j < n;j++)
if (Arr[j] < Arr[min_index])
Min_index = j;
Swap (&arr[i],&arr[min_index]);
}
Time complexity: O (n*n) space complexity: O (1)

3. Bubble sort
Code: void Bubblesort (int arr[],int N)
{
int i,j;
for (i = 0;i < n;i++)
for (j = 0;j < n-i-1;j++)
{
if (Arr[j] > Arr[j+1])
Swap (&arr[j],&arr[j+1]);
}
Time complexity: O (n*n) space complexity: O (1)

4. Insert Sort
Code: void Insertionsort (int arr[],int N)
{
int i,key,j;
for (i = 1;i < n;i++)
{
key = Arr[i];
j = i-1;
while (J >= 0 && Arr[j] > key)
{
ARR[J+1] = Arr[j];
j--;
}
ARR[J+1] = key;
}
Time complexity: O (n*n) space complexity: O (1)

5. Merge sort
Code: void Merge (int array[],int l,int m,int R)//merge two ordered arrays
{
int n1 = m-l + 1;
int n2 = r-m;
int TEMP1[N1];
int TEMP2[N2];
int i,j,k;
for (i = 0;i < n1;i++)
Temp1[i] = array[i+l];
for (j = 0;j < n2;j++)
TEMP2[J] = Array[m+1+j];
i = 0;
j = 0;
K = l;
while (I < N1 && J < N2)
{
if (Temp1[i] < temp2[j])
{
ARRAY[K] = Temp1[i];
i++;
k++;
}
Else
{
ARRAY[K] = Temp2[j];
j + +;
k++;
}
}
while (I < N1)
{
ARRAY[K] = Temp1[i];
i++;
k++;
}
while (J < N2)
{
ARRAY[K] = Temp2[j];
j + +;
k++;
}
}

void mergesort (int array[],int l,int R)
{
if (r-l > 0)
{
int m = l + (r-l)/2;
MergeSort (ARRAY,L,M);
MergeSort (ARRAY,M+1,R);
Merge (Array,l,m,r);
}
Time complexity: O (NLG (n)) spatial complexity: O (n)
6. Heap Sequencing
Ideas:Heap Sort algorithm for sorting in increasing order:
1.Build a max heap from the input data.
2.at this point, the largest item was stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of the heap by 1. Finally, heapify The root of the tree.
3.Repeat above steps until size of heap is greater than 1
Code:void maxheapify (struct maxheap * Maxheap,int index)
{
int left = index * 2 + 1;//Left dial hand node subscript
int right = Index * 2 + 2;//subscript
int maxindex = index;
if (left < maxheap->size && Maxheap->array[left] > Maxheap->array[index])
Maxindex = left;
if (Right < maxheap->size && Maxheap->array[right] > Maxheap->array[maxindex])
Maxindex = right;
if (maxindex! = index)
{
Swap (&maxheap->array[maxindex],&maxheap->array[index]);
Maxheapify (Maxheap,maxindex);
}
}

struct maxheap * CREATEANDBUILDHEAP (int * arr,int N)
{
int i;
struct Maxheap * maxheap = (struct maxheap*) malloc (sizeof (struct maxheap));
Maxheap->size = n;
Maxheap->array = arr;
for (i = (MAXHEAP-&GT;SIZE/2) -1;i >= 0;i--)//Start heapify operation from the largest parent node of the subscript
Maxheapify (Maxheap,i);
return maxheap;
}

void Headpsort (int arr[],int N)
{
struct Maxheap * maxheap = Createandbuildheap (arr,n);
while (Maxheap->size > 1)
{
Swap (&maxheap->array[0],&maxheap->array[maxheap->size-1]);
maxheap->size--;
Maxheapify (maxheap,0);
}
}
7. Quick Sort
Idea: Each time depending on the size of the last element in the array or the teenager array is divided into two parts, and then recursively to the two parts of the use of a fast row. Code: INT partition (int array[],int l,int h)
{
int i = L-1;
int target = array[h];
Int J;
for (j = l;j <= h-1;j++)
{
if (Array[j] <= target)
{
i++;
Swap (&array[i],&array[j]);
}
}
Swap (&array[i+1],&array[h]);
return i+1;
}

void QuickSort (int arr[],int l,int h)
{
if (H > L)
{
int pos = partition (ARR,L,H);
QuickSort (ARR,L,POS-1);
QuickSort (ARR,POS+1,H);
}
Time complexity: O (NLG (n))
8. Hill Sort
Code:void Shellsort (int arr[],int N)
{
int gap,i,j,temp;
for (gap = n/2;gap > 0;gap = GAP/2)
{
for (i = Gap;i < n;i++)
{
temp = Arr[i];
for (j = i;j >= gap && Arr[j-gap] > temp;j = j-gap)
ARR[J] = arr[j-gap];//Insert Sort
ARR[J] = temp;
}
}
}

Summary of 8 Classic sorting algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.