Write out the code that implements the sorting algorithm for
*Write out the code that implements the sorting algorithm for (selectionSort,insertionSort, bubbleSort, mergeSort, or quickSort).
Solution
The C code for SelectionSort is:
#include <stdio.h>
int main()
{
int A[] = {8, 53, 32, 54, 74, 3, 7, 18, 28, 37, 42, 42};
int count = 0, comparisons = 0;
for(int i = 0; i < 11; i++) //For each element.
{
int min = i; //Assumes the first element in the remaining array as min.
for(int j = i; j < 12; j++) //Find the position of least element in remaining array.
if(A[j] < A[min])
{
min = j;
comparisons++;
}
int temp = A[i]; //Exchange the first element in the array, with minimum element.
A[i] = A[min];
A[min] = temp;
count++;
for(int i = 0; i < 12; i++)
printf(\"%2i \", A[i]);
printf(\"\ \");
}
}
The C code for BubbleSort is:
#include<stdio.h>
void bubblesort(int array[],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(array[j]>array[j+1])
{
array[j]+=array[j+1];
array[j+1]=array[j]-array[j+1];
array[j]-=array[j+1];
}
}
int main()
{
int n,array[10],i;
printf(\"Enter the value of n: \");
scanf(\"%d\",&n);
printf(\"Enter %d elements:\",n);
for(i=0;i<n;i++)
scanf(\"%d\",&array[i]);
printf(\"\ Elements before sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
bubblesort(array,n);
printf(\"\ Elements after sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
}
The C code for InsertionSort is:
#include<stdio.h>
void insertionsort(int a[],int n)
{
int temp,i,j;
for(i=1;i<n;i++)
{
temp = a[i];
for(j=i-1;j>=0 && a[j] > temp;j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
int main()
{
int n,array[10],i;
printf(\"Enter the value of n: \");
scanf(\"%d\",&n);
printf(\"Enter %d elements:\",n);
for(i=0;i<n;i++)
scanf(\"%d\",&array[i]);
printf(\"\ Elements before sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
insertionsort(array,n);
printf(\"\ Elements after sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
}
The C code for MergeSort is:
#include<stdio.h>
void merge(int a[],int low,int mid,int high)
{
int first = low, second = mid,b[10],k=0;
while(low < second && mid <=high)
{
if(a[low] < a[mid])
b[k++] = a[low++];
else
b[k++] = a[mid++];
}
while(low < second)
b[k++] = a[low++];
while(mid <= high)
b[k++] = a[mid++];
k = 0;
while(first <=high)
{
a[first] = b[k];
first++;
k++;
}
}
void mergesort(int a[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid+1,high);
}
}
int main()
{
int n,array[10],i;
printf(\"Enter the value of n: \");
scanf(\"%d\",&n);
printf(\"Enter %d elements:\",n);
for(i=0;i<n;i++)
scanf(\"%d\",&array[i]);
printf(\"\ Elements before sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
mergesort(array,0,n-1);
printf(\"\ Elements after sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
}
The C code for QuickSort is:
#include<stdio.h>
void quicksort(int array[],int low,int high)
{
int pivot;
if(low < high)
{
pivot = quick(array,low,high);
quicksort(array,low,pivot-1);
quicksort(array,pivot+1,high);
}
}
int quick(int array[],int low,int high)
{
int pivot,rb;
pivot = low;
rb = high;
while(low <=high)
{
while(array[pivot] >= array[low] && low <=rb)
low++;
while(array[pivot] < array[high])
high--;
if(pivot != high)
{
array[pivot] = array[pivot] + array[high];
array[high ] = array[pivot] - array[high];
array[pivot] = array[pivot] - array[high];
}
}
return high;
}
int main()
{
int n,array[10],i;
printf(\"Enter the value of n: \");
scanf(\"%d\",&n);
printf(\"Enter %d elements:\",n);
for(i=0;i<n;i++)
scanf(\"%d\",&array[i]);
printf(\"\ Elements before sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
quicksort(array,0,n-1);
printf(\"\ Elements after sorting: \");
for(i=0;i<n;i++)
printf(\"%d \",array[i]);
}



