Sorting Program
Sorting Program
Program:9 Design and implement C/C++ Program to sort a given set of n integer
elements using Selection Sort method and compute its time complexity. Run the
program for varied values of n> 5000 and record the time taken to sort. Plot a graph of
the time taken versus n. The elements can be read from a file or can be generated using
the random number generator.
Aim: Sort a given set of elements using Selection sort and determine the time required to sort
elements. Repeat the experiment for different values of n, the number of elements in the list to be
sorted and plot a graph of the time taken versus n.
Definition: selection sort is a sorting routine that scans a list of items repeatedly and, on each
pass, selects the item with the lowest value and places it in its final position. It is based on brute
force approach. Sequential search is a Θ(n2) algorithm on all inputs.
Algorithm:
SelectionSort(A[0…n-1])
//sort a given array by select5ion sort
//input:A[0…n-1]of orderable elements
Output:Array a[0…n-1] Sorted in ascending order
for i<- 0 to n-2 do
min<-i
for j<-i+1 to n-1 do
if A[j]<A[min] min<-j
swap A[i] and A[min]
void fnGenRandInput(int [], int); void fnDispArray( int [], int); void fnSelectionSort(int
[], int);
FILE *fp;
struct timeval tv; double dStart,dEnd;
int iaArr[500000],iNum,i,iChoice;
for(;;)
{
printf("\n 1.Plot the Graph\n 2.Selection Sort\n 3.Exit"); printf("\n Enter your choice\n");
scanf("%d",&iChoice);
for(i=100;i<20000;i+=100)
{
fnGenRandInput(iaArr,i);
gettimeofday(&tv,NULL);
dStart = tv.tv_sec + (tv.tv_usec/1000000.0);
fnSelectionSort(iaArr,i);
gettimeofday(&tv,NULL);
dEnd = tv.tv_sec + (tv.tv_usec/1000000.0);
fprintf(fp,"%d\t%lf\n",i,dEnd-dStart);
}
fclose(fp);
printf("\nData File generated and stored in file < SelectionPlot.dat >\n Use a plotting
utility\n"); break;
case 2:
printf("\nEnter the number of elements to sort\n"); scanf("%d",&iNum);
printf("\nUnsorted Array\n"); fnGenRandInput(iaArr,iNum); fnDispArray(iaArr,iNum);
fnSelectionSort(iaArr,iNum); printf("\nSorted Array\n"); fnDispArray(iaArr,iNum);
break;
case 3:
exit(0);
}
}
return 0;
}
srand(time(NULL)); for(i=0;i<n;i++)
{
X[i] = rand()%10000;
}
for(i=0;i<n;i++)
printf(" %5d \n",X[i]);
Selectionsort.gpl
set terminal png font arial
set title "Time Complexity for Selection Sort" set autoscale
set xlabel "Size of Input"
set ylabel "Sorting Time (microseconds)" set grid
set output "SelectionPlot.png"
plot "SelectionPlot.dat" t 'Selection Sort' with lines
OUTPUT
1. Plot the Graph
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
2. Selection Sort
3. Exit
Enter your choice 2
5579
4966
4037
2660
7868
5762
6749
3345
5713
2. Selection Sort
3. Exit
Enter your choice 1
Data File generated and stored in file < SelectionPlot.dat > Use a plotting utility
Program: 10 Design and implement C/C++ Program to sort a given set of n integer
elements using Quick Sort method and compute its time complexity. Run the program
for varied values of n> 5000 and record the time taken to sort. Plot a graph of the time
taken versus n. The elements can be read from a file or can be generated using the
random number generator.
Aim:
The aim of this program is to sort ‘n’ randomly generated elements using Quick sort and
Plotting the graph of the time taken to sort n elements versus n.
Definition: Quick sort is based on the Divide and conquer approach. Quick sort divides array
according to their value. Partition is the situation where all the elements before some position s
are smaller than or equal to A[s] and all the elements after position s are greater than or equal to
A[s].
for(;;)
{
printf("\n1.Plot the Graph\n2.QuickSort\n3.Exit"); printf("\nEnter your choice\n");
scanf("%d",&iChoice); switch(iChoice)
{
case 1:
fp = fopen("QuickPlot.dat","w");
for(i=100;i<100000;i+=100)
{
fnGenRandInput(iaArr,i); gettimeofday(&tv,NULL);
dStart = tv.tv_sec + (tv.tv_usec/1000000.0); fnQuickSort(iaArr,0,i-1);
gettimeofday(&tv,NULL);
dEnd = tv.tv_sec + (tv.tv_usec/1000000.0); fprintf(fp,"%d\t%lf\n",i,dEnd-dStart);
}
fclose(fp);
printf("\nData File generated and stored in file < QuickPlot.dat >.\n Use a plotting
utility\n"); break;
case 2:
printf("\nEnter the number of elements to sort\n"); scanf("%d",&iNum);
printf("\nUnsorted Array\n"); fnGenRandInput(iaArr,iNum); fnDispArray(iaArr,iNum);
fnQuickSort(iaArr,0,iNum-1); printf("\nSorted Array\n"); fnDispArray(iaArr,iNum);
break;
case 3:
exit(0);
}
}
return 0;
}
p = a[l];
i = l;
j = r+1; do
{
do { i++; } while (a[i] < p);
do { j--; } while (a[j] > p);
fnSwap(&a[i], &a[j]);
}while (i<j);
fnSwap(&a[i], &a[j]);
fnSwap(&a[l], &a[j]); return j;
}
Quicksort.gpl
set terminal png font arial
set title "Time Complexity for Quick Sort" set autoscale
set xlabel "Size of Input"
set ylabel "Sorting Time (microseconds)" set grid
set output "QuickPlot.png"
plot "QuickPlot.dat" t 'Quick Sort' with lines
OUTPUT:
1.Plot the Graph 2.QuickSort
3.Exit
Enter your choice 2
Unsorted Array
2220
3635
527
9636
2474
1241
89
8370
3904
3668
Data File generated and stored in file < QuickPlot.dat >. Use a plotting utility
Program 11: Design and implement C/C++ Program to sort a given set of n integer
elements using Merge Sort method and compute its time complexity. Run the program
for varied values of n> 5000, and record the time taken to sort. Plot a graph of the time
taken versus n. The elements can be read from a file or can be generated using the
random number generator.
Aim:
The aim of this program is to sort ‘n’ randomly generated elements using Merge sort and
Plotting the graph of the time taken to sort n elements versus n.
Definition: Merge sort is a sort algorithm based on divide and conquer technique. It divides the
array element based on the position in the array. The concept is that we first break the list into two
smaller lists of roughly the same size, and then use merge sort recursively on the subproblems,
until they cannot subdivide anymore. Then, we can merge by stepping through the lists in linear
time.
Its time efficiency is Θ(n log n).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
FILE *fp;
double dStart,dEnd;
int iaArr[500000],iNum,i,iChoice;
for(;;)
switch(iChoice)
case 1:
fp = fopen("MergePlot.dat","w");
for(i=100;i<100000;i+=100)
fnGenRandInput(iaArr,i);
gettimeofday(&tv,NULL);
fnMergeSort(iaArr,0,i-1);
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
gettimeofday(&tv,NULL);
fprintf(fp,"%d\t%lf\n",i,dEnd-dStart);
fclose(fp);
printf("\nData File generated and stored in file < MergePlot.dat >.\n Use a plotting
utility\n"); break;
case 2:
break;
case 3:
exit(0);
return 0;
j=mid+1;
{
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
while(i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(i=low;i<k;i++) a[i]=b[i];
int mid;
if(low<high)
mid=(low+high)/2; fnMergeSort(a,low,mid);
fnMergeSort(a,mid+1,high); fnMerge(a,low,mid,high);
int i;
srand(time(NULL)); for(i=0;i<n;i++)
X[i] = rand()%10000;
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
}
int i;
for(i=0;i<n;i++)
Mergesort.gpl
OUTPUT
2115
185
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
5107
2818
3046
7274
8511
5897
1864
1864
2115
2818
3046
5095
5107
5897
7274
8511
2. MergeSort 3.Exit