0% found this document useful (0 votes)
34 views

Sorts

The document describes 6 sorting algorithms: selection sort, insertion sort, heap sort, bubble sort, and quick sort. Selection sort finds the minimum value in the array and swaps it with the first element. Insertion sort iterates through the array, inserting each element into its sorted position. Heap sort uses a binary heap to sort the array in O(nlogn) time. Bubble sort iterates through the array, continuously swapping adjacent elements that are out of order. Quick sort chooses a pivot element and partitions the array into subarrays based on element values relative to the pivot.

Uploaded by

Vijay Paulraj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Sorts

The document describes 6 sorting algorithms: selection sort, insertion sort, heap sort, bubble sort, and quick sort. Selection sort finds the minimum value in the array and swaps it with the first element. Insertion sort iterates through the array, inserting each element into its sorted position. Heap sort uses a binary heap to sort the array in O(nlogn) time. Bubble sort iterates through the array, continuously swapping adjacent elements that are out of order. Quick sort chooses a pivot element and partitions the array into subarrays based on element values relative to the pivot.

Uploaded by

Vijay Paulraj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Selection sort

void SelectionSort(int a[], int array_size)


{
int i;
for (i = 0; i < array_size - 1; ++i)
{
int j, min, temp;
min = i;
for (j = i+1; j < array_size; ++j)
{
if (a[j] < a[min])
min = j;
}

temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

insertion sort

void insertionSort(int a[], int array_size)


{
int i, j, index;
for (i = 1; i < array_size; ++i)
{
index = a[i];
for (j = i; j > 0 && a[j-1] > index; j--)
a[j] = a[j-1];

a[j] = index;
}
}

heap sort

void downHeap(int a[], int root, int bottom)


{
int maxchild, temp, child;
while (root*2 < bottom)
{
child = root * 2 + 1;
if (child == bottom)
{
maxchild = child;
}
else
{
if (a[child] > a[child + 1])
maxchild = child;
else
maxchild = child + 1;
}

if (a[root] < a[maxchild])


{
temp = a[root];
a[root] = a[maxchild];
a[maxchild] = temp;
}
else return;

root = maxchild;
}
}

bubble sort

void BubbleSort(int a[], int array_size)


{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}

quick sort

#include <stdio.h>
void swap(int *x,int *y)
{
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
}
 
void quicksort(int list[],int m,int n)
{
   int key,i,j,k;
   if( m < n)
   {
      
k = ((m+n)/2; // choosing pivot element
      swap(&list[m],&list[k]);
      key = list[m];
      i = m+1;
      j = n;
      while(i <= j)
      {
         while((i <= n) && (list[i] <= key))
                i++;
         while((j >= m) && (list[j] > key))
                j--;
         if( i < j)
                swap(&list[i],&list[j]);
      }
      swap(&list[m],&list[j]);
      // recursively sort the lesser list
      quicksort(list,m,j-1);
      quicksort(list,j+1,n);
   }
}

You might also like