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

Sorting Program

The document outlines a series of C/C++ programming assignments focused on sorting algorithms, including Selection Sort, Quick Sort, and Merge Sort. Each program aims to sort a set of randomly generated integers, measure the time complexity, and plot the results against varying input sizes. The algorithms are implemented with specific methods for generating random input, displaying arrays, and recording execution time for analysis.

Uploaded by

gowdasihi536
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)
6 views

Sorting Program

The document outlines a series of C/C++ programming assignments focused on sorting algorithms, including Selection Sort, Quick Sort, and Merge Sort. Each program aims to sort a set of randomly generated integers, measure the time complexity, and plot the results against varying input sizes. The algorithms are implemented with specific methods for generating random input, displaying arrays, and recording execution time for analysis.

Uploaded by

gowdasihi536
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/ 16

ADA LABORATORY(BCSL404)

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]

#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h>

void fnGenRandInput(int [], int); void fnDispArray( int [], int); void fnSelectionSort(int
[], int);

int main( int argc, char **argv)


{

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);

Dept of CSE, SCE


ADA LABORATORY(BCSL404)
switch(iChoice)
{
case 1:
fp = fopen("SelectionPlot.dat","w");

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;
}

void fnGenRandInput(int X[], int n)


{
int i;
Dept of CSE, SCE
ADA LABORATORY(BCSL404)

srand(time(NULL)); for(i=0;i<n;i++)
{
X[i] = rand()%10000;
}

void fnDispArray( int X[], int n)


{
int i;

for(i=0;i<n;i++)
printf(" %5d \n",X[i]);

void fnSelectionSort(int X[], int n)


{
int i,j,iTemp,iMin; for(i=0;i<n;i++)
{
iMin = i;
for(j = i+1;j<n;j++)
{
if(X[j] < X[iMin])
iMin = j;
}

iTemp = X[i]; X[i] = X[iMin]; X[iMin] = iTemp;


}
}

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

Enter the number of elements to sort 10

Unsorted Array 5849

5579
4966
4037
2660
7868
5762
6749
3345
5713

Sorted Array 2660


3345
4037
4966
5579
5713
5762
5849
6749
7868

1. Plot the Graph

2. Selection Sort
3. Exit
Enter your choice 1

Data File generated and stored in file < SelectionPlot.dat > Use a plotting utility

1. Plot the Graph


2. Selection Sort
3. Exit
Enter your choice 3
root@comp14:~/Desktop# gnuplot selectionsortt.gpl

Dept of CSE, SCE


ADA LABORATORY(BCSL404)

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].

Efficiency: Cbest(n) Є Θ(nlog2n),Cworst(n) ЄΘ(n2),Cavg(n)Є1.38nlog2n

Dept of CSE, SCE


ADA LABORATORY(BCSL404)
Algorithm: Quick sort (A[l….r])
// Sorts a sub array by quick sort
//Input : A sub array A[l..r] of A[0..n-1] ,defined by its left and right indices l
//and r
// Output : The sub array A[l..r] sorted in non decreasing order
if l < r
s = Partition (A[l..r]) //s is a split position
Quick sort (A[l …s-1])
Quick sort (A[s+1…r])

ALGORITHM Partition (A[l…r])


//Partition a sub array by using its first element as a pivot
// Input : A sub array A[l…r] of A[0…n-1] defined by its left and right indices l and // r (l < r)
// Output : A partition of A[l…r], with the split position returned as this function’s value
p=A[l]
i=l;
j=r+1;
repeat
delay(500);
repeat i= i+1 until A[i] >= p
repeat j=j-1 until A[J] <= p
Swap (A[i],A[j])
until I >=j
Swap (A[i],A[j]) // Undo last Swap when i>= j
Swap (A[l],A[j])
Return j

#include <stdio.h> #include <stdlib.h>


#include <sys/time.h> #include <time.h>

void fnGenRandInput(int [], int); void fnDispArray( int [], int);


int fnPartition(int [], int , int); void fnQuickSort(int [], int , int); void fnSwap(int*, int*);

void fnSwap(int *a, int *b)


{
int t = *a; *a = *b; *b = t;
}

int main( int argc, char **argv)


{
FILE *fp;
struct timeval tv; double dStart,dEnd;
int iaArr[500000],iNum,i,iChoice;

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");

Dept of CSE, SCE


ADA LABORATORY(BCSL404)

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;
}

int fnPartition(int a[], int l, int r)


{
int i,j; int p;

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;
}

void fnQuickSort(int a[], int l, int r)


{
int s;
if (l < r)
{
s = fnPartition(a, l, r); fnQuickSort(a, l, s-1); fnQuickSort(a, s+1, r);
}
Dept of CSE, SCE
ADA LABORATORY(BCSL404)

void fnGenRandInput(int X[], int n)


{
srand(time(NULL)); for(int i=0;i<n;i++)
{
X[i] = rand()%10000;
}
}

void fnDispArray( int X[], int n)


{
for(int i=0;i<n;i++)
printf(" %5d \n",X[i]);
}

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

Enter the number of elements to sort 10

Unsorted Array

2220
3635
527
9636
2474
1241
89
8370
3904
3668

Dept of CSE, SCE


ADA LABORATORY(BCSL404)
Sorted Array 89
527
1241
2220
2474
3635
3668
3904
8370
9636

1.Plot the Graph 2.QuickSort


3.Exit
Enter your choice 1

Data File generated and stored in file < QuickPlot.dat >. Use a plotting utility

1.Plot the Graph 2.QuickSort


3.Exit
Enter your choice 3
root@comp14:~/Desktop# gnuplot Quicksort.gpl

Dept of CSE, SCE


ADA LABORATORY(BCSL404)

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).

Algorithm: Merge sort (A[0…n-1]


// Sorts array A[0..n-1] by Recursive merge sort
// Input : An array A[0..n-1] elements
// Output : Array A[0..n-1] sorted in non decreasing order
If n > 1
Copy A[0…(n/2)-1] to B[0…(n/2)-1]
Copy A[0…(n/2)-1] to C[0…(n/2)-1]
Mergesort (B[0…(n/2)-1])
Mergesort (C[0…(n/2)-1])
Merge(B,C,A)

ALGORITHM Merge (B[0…p-1], C[0…q-1],A[0….p+q-1])


// merges two sorted arrays into one sorted array
// Input : Arrays B[0..p-1] and C[0…q-1] both sorted
// Output : Sorted array A[0…. p+q-1] of the elements of B and C
I = 0;
J = 0;
K= 0;
While I < p and j < q do
If B[i] <= C[j]
A[k]= B[I]; I= I+1;
Else
A[k] = B[i]; I=i+1
K=k+1;
If I = = p
Copy C[ j..q-1] to A[k….p+q-1]
else
Copy B[I … p-1] to A[k …p+q-1

#include <stdio.h>

#include <stdlib.h>

Dept of CSE, SCE


ADA LABORATORY(BCSL404)
#include <sys/time.h>

#include <time.h>

void fnGenRandInput(int [], int);

void fnDispArray( int [], int);

void fnMerge(int [], int ,int ,int);

void fnMergeSort(int [], int , int);

int main( int argc, char **argv)

FILE *fp;

struct timeval tv;

double dStart,dEnd;

int iaArr[500000],iNum,i,iChoice;

for(;;)

printf("\n1.Plot the Graph\n2.MergeSort\n3.Exit");

printf("\nEnter your choice\n"); scanf("%d",&iChoice);

switch(iChoice)

case 1:

fp = fopen("MergePlot.dat","w");

for(i=100;i<100000;i+=100)

fnGenRandInput(iaArr,i);

gettimeofday(&tv,NULL);

dStart = tv.tv_sec + (tv.tv_usec/1000000.0);

fnMergeSort(iaArr,0,i-1);
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
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 < MergePlot.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);


fnMergeSort(iaArr,0,iNum-1); printf("\nSorted Array\n"); fnDispArray(iaArr,iNum);

break;

case 3:

exit(0);

return 0;

void fnMerge(int a[], int low,int mid,int high)

int i,k,j,b[500000]; i=k=low;

j=mid+1;

while(i<=mid && j<=high)

{
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];

void fnMergeSort(int a[],int low,int high)

int mid;

if(low<high)

mid=(low+high)/2; fnMergeSort(a,low,mid);

fnMergeSort(a,mid+1,high); fnMerge(a,low,mid,high);

void fnGenRandInput(int X[], int n)

int i;

srand(time(NULL)); for(i=0;i<n;i++)

X[i] = rand()%10000;
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
}

void fnDispArray( int X[], int n)

int i;

for(i=0;i<n;i++)

printf(" %5d \n",X[i]);

Mergesort.gpl

set terminal png font arial

set title "Time Complexity for Merge Sort" set autoscale

set xlabel "Size of Input"

set ylabel "Sorting Time (microseconds)" set grid

set output "MergePlot.png"

plot "MergePlot.dat" t 'Merge Sort' with lines

OUTPUT

1.Plot the Graph 2.MergeSort 3.Exit

Enter your choice 2

Enter the number of elements to sort 10

Unsorted Array 5095

2115

185
Dept of CSE, SCE
ADA LABORATORY(BCSL404)
5107

2818

3046

7274

8511

5897

1864

Sorted Array 185

1864

2115

2818

3046

5095

5107

5897

7274

8511

1.Plot the Graph 2.MergeSort 3.Exit

Enter your choice 1


Dept of CSE, SCE
ADA LABORATORY(BCSL404)
Data File generated and stored in file < MergePlot.dat >. Use a plotting utility

1. Plot the Graph

2. MergeSort 3.Exit

Enter your choice 3

root@comp14:~/Desktop# gnuplot mergesortt.gpl

Dept of CSE, SCE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy