[go: up one dir, main page]

0% found this document useful (0 votes)
16 views11 pages

Sort Programs New

The document contains implementations of three sorting algorithms: Selection Sort, Merge Sort, and Quick Sort in C. Each algorithm includes a main function that generates an array of random integers, sorts the array, and measures the time taken to sort. The code also includes error handling for array size limits and displays the unsorted and sorted arrays.

Uploaded by

Saanvi Ks
Copyright
© © All Rights Reserved
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)
16 views11 pages

Sort Programs New

The document contains implementations of three sorting algorithms: Selection Sort, Merge Sort, and Quick Sort in C. Each algorithm includes a main function that generates an array of random integers, sorts the array, and measures the time taken to sort. The code also includes error handling for array size limits and displays the unsorted and sorted arrays.

Uploaded by

Saanvi Ks
Copyright
© © All Rights Reserved
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/ 11

1.

Selection Sort:

#include <stdio.h>

#include <stdlib.h>

#include <time.h> // Include time.h for clock()

#define MAX 5000

void selectionsort(int a[],int n)

int i, j, min;

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

min = i;

for(j = i+1; j<n; j++)

if(a[j]<a[min])

min= j;

// Swap the found minimum element with the first element

int temp = a[min];

a[min] = a[i];

a[i] = temp;

int main()

int a[MAX], n, i;
printf("Enter number of elements to be sorted ");

scanf("%d", &n);

if (n > MAX)

printf("Size exceeds the maximum limit.\n");

return 1;

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

a[i] =rand()%10000;

printf("UNSORTED ARRAY\n");

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

printf("%d\t",a[i]);

clock_t start_time =clock();

printf("\nSorted array:\t \n");

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

selectionsort(a,n);

printf("%5d\t", a[i]);

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

selectionsort(a,n);

// Measure time after sorting

clock_t end_time =clock();

// Calculate time taken

double time_taken = ((double)(end_time-start_time))/CLOCKS_PER_SEC;

printf("\nTime taken to sort for element %d is %.6f seconds\n ", a[i],time_taken);


}

}
10. Merge Sort:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 10000

void merge(int array[], int low, int mid, int high)

int i = low;

int j = mid + 1;

int k = low;

int resarray[MAX];

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

if (array[i] < array[j])

resarray[k] = array[i];

i++;

else

resarray[k] = array[j];

j++;

k++;

}
while (i <= mid)

resarray[k++] = array[i++];

while (j <= high)

resarray[k++] = array[j++];

for (int m = low; m <= high; m++)

array[m] = resarray[m];

void sort(int array[], int low, int high)

if (low < high)

int mid = (low + high) / 2;

sort(array, low, mid);

sort(array, mid + 1, high);

merge(array, low, mid, high);

int main()

int array[MAX];

int n, i;

printf("Enter the array size: ");

scanf("%d", &n);

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

array[i] = rand() % 10000;

clock_t startTime = clock();


printf("Array before sorting:\n");

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

printf("%d ", array[i]);

printf("\nArray after sorting:\n");

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

sort(array, 0, n - 1);

printf("%d ", array[i]);

clock_t stopTime = clock();

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

clock_t stopTime = clock();

double elapsedTime = ((double)(stopTime - startTime)) / CLOCKS_PER_SEC;

printf("\nTime taken to sort array element %d is: %.6f seconds\n",array[i],elapsedTime);

}
11. Quick Sort

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 2000

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int array[], int low, int high) {


int pivot = array[high];

int i = (low - 1);

for (int j = low; j < high; j++) {

if (array[j] < pivot) {

i++;

swap(&array[i], &array[j]);

swap(&array[i + 1], &array[high]);

return (i + 1);

void quickSort(int array[], int low, int high) {

if (low < high) {

int pi = partition(array, low, high);

quickSort(array, low, pi - 1);

quickSort(array, pi + 1, high);

int main() {

int array[MAX];

int n, i;

printf("Enter the array size: ");


scanf("%d", &n);

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

array[i] = rand() % 10000;

clock_t startTime = clock();

printf("Array before sorting:\n");

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

printf("%d ", array[i]);

printf("\n");

quickSort(array, 0, n - 1);

printf("Array after sorting:\n");

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

printf("%d ", array[i]);

printf("\n");

clock_t stopTime = clock();

double elapsedTime = ((double)(stopTime - startTime)) / CLOCKS_PER_SEC;

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

printf("\nTime taken to sort for element %d is %.6f seconds\n ", array[i],elapsedTime);

return 0;

You might also like