Assignment 3 Solution PDF
Assignment 3 Solution PDF
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
LARGE_INTEGER frequency;
LARGE_INTEGER start;
LARGE_INTEGER end;
#define MAX_SIZE 10
void SWAP(int arr[], int x, int y)
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
// Bubble sort
void bubble_sort(int arr[])
{
int i, j, temp;
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
// Insertion_sort
void insertion_sort(int arr[]) {
int i, j, key;
// Merge sort
void Merge(int arr[], int left, int mid, int right)
{
int i, j, k, l;
int sort_arr[MAX_SIZE];
i = left;
j = mid + 1;
k = left;
if (i > mid) {
for (l = j; l <= right; l++)
{
sort_arr[k++] = arr[k];
}
}
else {
for (l = i; l <= mid; l++)
sort_arr[k++] = arr[l];}
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
for (l = left; l <= right; l++)
{
arr[l] = sort_arr[l];
}
// Quick Sort
int Partition(int arr[], int left, int right)
{
int pivot = arr[left];
int low = left + 1;
int high = right;
}
SWAP(arr, left, high);
return high;
}
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
// Radix Sort
void Radix_Sort(int arr[])
{
int radix[MAX_SIZE];
int max = 0;
int exp = 1;
int bucket[MAX_SIZE] = { 0, };
for (int i = 0; i < MAX_SIZE; i++)
bucket[arr[i] / exp % 10]++;
exp *= 10;
}
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
// Main
void main()
{
int rand_arr[MAX_SIZE];
double res;
// Bubble Sort
rand_num(rand_arr);
//Insertion Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency); //microsecond
QueryPerformanceCounter(&start);
insertion_sort(rand_arr); // sort
QueryPerformanceCounter(&end);
//Merge Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);//microsecond
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
merge_sort(rand_arr, 0, MAX_SIZE - 1); //sort
QueryPerformanceCounter(&end);
//Quick Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
QuickSort(rand_arr, 0, MAX_SIZE - 1); //sort
QueryPerformanceCounter(&end);
//Radix Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
Radix_Sort(rand_arr); //sort
QueryPerformanceCounter(&end);
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;
//Buctet Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
Bucket_sort(rand_arr); //sort
QueryPerformanceCounter(&end);
This study source was downloaded by 100000803655387 from CourseHero.com on 04-02-2022 14:42:07 GMT -05:00
https://www.coursehero.com/file/63050866/Assignment-3-Solutionpdf/
Powered by TCPDF (www.tcpdf.org)