0% found this document useful (0 votes)
5 views4 pages

Sortings

Uploaded by

Yogi Nambula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Sortings

Uploaded by

Yogi Nambula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sorting Techniques

#include <stdio.h>
#include <stdlib.h>
int i,j;
void swap(int *p, int *q){
int t;
t=*p;
*p=*q;
*q=t;
}
void selectionSort(int list[], int n){
int mi;
for(i=0;i<n-1;i++){
mi=i;
for(j=i+1;j<n;j++)
if(list[j]<list[mi])
mi=j;
swap(&list[mi],&list[i]);
}
}
void insertionSort(int list[], int n){
int key;
for(i=1;i<n;i++){
key=list[i];
for(j=i-1;j>=0 && list[j]>key;j--)
list[j+1]=list[j];
list[j+1]=key;
}
}
void shellSort(int list[], int n){
int gap;
for(gap=n/2;gap>0;gap/=2){
for(i=gap;i<n;i++)
for(j=i-gap;j>=0;j-=gap)
if(list[j]<list[j+gap])
break;
else
swap(&list[j],&list[j+gap]);
}
}
void display(int list[],int n){
for(i=0;i<n;i++)
printf("%d ",list[i]);
}
int partition(int list[], int low, int high) {
int pivot = list[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (list[j] <= pivot) {
i++;
swap(&list[i], &list[j]);
}
}
swap(&list[i + 1], &list[high]);
return (i + 1);
}
void quickSort(int list[], int low, int high) {
if (low < high) {
int pi = partition(list, low, high);
quickSort(list, low, pi - 1);
quickSort(list, pi + 1, high);
}
}
void heapify(int list[], int n, int i){
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if(left<n&&list[left]>list[largest])
largest = left;
if(right<n&&list[right]>list[largest])
largest = right;
if(largest!=i){
swap(&list[i], &list[largest]);
heapify(list, n, largest);
}
}
void heapSort(int list[], int n) {
for(i=n/2-1;i>=0;i--)
heapify(list, n, i);
for(i=n-1;i>= 0;i--){
swap(&list[0], &list[i]);
heapify(list, i, 0);
}
}
void merge(int list[], int l, int m, int r){
int k;
int n1=m-l+1;
int n2=r-m;
int L[n1],R[n2];
for(i=0;i<n1;i++)
L[i] = list[l + i];
for(j=0;j<n2;j++)
R[j] = list[m + 1+ j];
i = 0;
j = 0;
k = l;
while(i<n1&&j<n2){
if(L[i]<=R[j]){
list[k] = L[i];
i++;
}
else{
list[k] = R[j];
j++;
}
k++;
}
while(i<n1){
list[k] = L[i];
i++;
k++;
}
while(j<n2){
list[k] = R[j];
j++;
k++;
}
}
void mergeSort(int list[], int l, int r){
if (l < r){
int m = l+(r-l)/2;
mergeSort(list, l, m);
mergeSort(list, m+1, r);
merge(list, l, m, r);
}
}
int main()
{
int list[10],n;
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
do{
printf("\n1. Sort the Elements using Insertion Sort. \n2. Sort the Elements using
ISelection Sort. \n3. Sort the Elements using Shell Sort. \n4. Sort the Elements using Quick
Sort. \n5. Sort the Elements using Merge Sort. \n6. Sort the Elements using Heap Sort. \n7.
Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&j);
switch(j){
case 1: insertionSort(list,n);
printf("\nElements after applying Insertion Sort: ");
display(list,n);
printf("\n");
break;
case 2: selectionSort(list,n);
printf("\nElements after applying Selection Sort: ");
display(list,n);
printf("\n");
break;
case 3: shellSort(list,n);
printf("\nElements after applying Shell Sort: ");
display(list,n);
printf("\n");
break;
case 4:quickSort(list,0,n-1);
printf("\nElements after applying Quick Sort: ");
display(list,n);
printf("\n");
break;
case 5:mergeSort(list,0,n-1);
printf("\nElements after applying Merge Sort: ");
display(list,n);
printf("\n");
break;
case 6:heapSort(list,n);
printf("\nElements after applying Heap Sort: ");
display(list,n);
printf("\n");
break;
case 7: exit(0);

}
}while(1);
return 0;
}

You might also like