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

Some Sorting Algorithms Using C++

The document contains a C++ program that implements three sorting algorithms: selection sort, insertion sort, and bubble sort. It generates an array of random integers and measures the execution time for each sorting algorithm on the array. The user can change the size of the array to observe the corresponding execution times.
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)
3 views2 pages

Some Sorting Algorithms Using C++

The document contains a C++ program that implements three sorting algorithms: selection sort, insertion sort, and bubble sort. It generates an array of random integers and measures the execution time for each sorting algorithm on the array. The user can change the size of the array to observe the corresponding execution times.
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

// sorting algorithms

#include <iostream>
#include <ctime>
using namespace std;

#define arrSize 1000 /*Please change the size of the array to different values (10,100,1000,
10000,1000000) to observe the corresponding execution times for each size.*/

void swap(int &a, int &b);


void selectionSort(int arr[], int size);
void insertionSort(int arr[], int size);
void bubbleSort(int arr[], int size) ;

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


int temp = a;
a = b;
b = temp;
}

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


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

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


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

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


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

1
int main() {

clock_t start1,end1;
double time1;int i;
int arr[arrSize];
// Generate array of random integer values between 0 and 99
for (i = 0; i < arrSize; i++) {
arr[i] = rand() % 100;
}

// Sort using selection sort


cout<<"Sort using selection sort"<<endl;
start1 = clock();
selectionSort(arr, arrSize);
end1 = clock();
time1 = double(end1 - start1) / CLOCKS_PER_SEC * 1000000; // 1000.0 for milliseconds, 1000000 for
microseconds
cout<<"The time taken to sort the array using selection sort for "<<arrSize<<" elements is
"<<time1<<" Microseconds"<<endl;

// Sort using selection sort


cout<<"Sort using inserion sort"<<endl;
start1 = clock();
insertionSort(arr, arrSize);
end1 = clock();
time1 = double(end1 - start1) / CLOCKS_PER_SEC * 1000000; // 1000.0 for milliseconds, 1000000 for
microseconds
cout<<"The time taken to sort the array using insertion sort for "<<arrSize<<" is "<<time1<<"
Microseconds"<<endl;

// Sort using selection sort


cout<<"Sort using buble sort"<<endl;
start1 = clock();
bubbleSort(arr, arrSize);
end1 = clock();
time1= double(end1 - start1) / CLOCKS_PER_SEC * 1000000; // 1000.0 for milliseconds, 1000000 for
microseconds
cout<<"The time taken to sort the array using bubble sort for "<<arrSize<<" is "<<time1<<"
Microseconds"<<endl;

cout << endl;


return 0;
}

You might also like