0% found this document useful (0 votes)
4 views2 pages

C Sorting Algorithms Performance Test

The document contains a C program that implements three sorting algorithms: bubble sort, selection sort, and insertion sort. It generates an array of 10,000 random integers and measures the execution time of each sorting algorithm. The results are printed in milliseconds for comparison of their performance.

Uploaded by

omerfaruknar422
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)
4 views2 pages

C Sorting Algorithms Performance Test

The document contains a C program that implements three sorting algorithms: bubble sort, selection sort, and insertion sort. It generates an array of 10,000 random integers and measures the execution time of each sorting algorithm. The results are printed in milliseconds for comparison of their performance.

Uploaded by

omerfaruknar422
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

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10000

void bubbleSort(int arr[], int size) {


for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-i-1; j++) {
if (arr[j] > arr[j+1]) {
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}

void selectionSort(int arr[], int size) {


for (int i = 0; i < size-1; i++) {
int min_idx = i;
for (int j = i+1; j < size; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int tmp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = tmp;
}
}

void insertionSort(int arr[], int size) {


for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}

int main() {
srand(time(NULL));
int arr1[SIZE];
int arr2[SIZE];
int arr3[SIZE];

for (int i = 0; i < SIZE; i++) {


int val = rand() % 100000;
arr1[i] = val;
arr2[i] = val;
arr3[i] = val;
}

clock_t start = clock();


insertionSort(arr1, SIZE);
clock_t end = clock();
printf("Insertion Sort time: %.2f ms\n", 1000.0 * (end -
start) / CLOCKS_PER_SEC);

start = clock();
selectionSort(arr2, SIZE);
end = clock();
printf("Selection Sort time: %.2f ms\n", 1000.0 * (end -
start) / CLOCKS_PER_SEC);

start = clock();
bubbleSort(arr3, SIZE);
end = clock();
printf("Bubble Sort time: %.2f ms\n", 1000.0 * (end - start)
/ CLOCKS_PER_SEC);

return 0;
}

You might also like