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

Assignment 3 Solution PDF

This document discusses and provides code implementations for several sorting algorithms including bubble sort, insertion sort, merge sort, quick sort, radix sort, and bucket sort. It generates random arrays of numbers, sorts them using each algorithm, and times the sorting performance of each approach.

Uploaded by

공예나
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)
52 views

Assignment 3 Solution PDF

This document discusses and provides code implementations for several sorting algorithms including bubble sort, insertion sort, merge sort, quick sort, radix sort, and bucket sort. It generates random arrays of numbers, sorts them using each algorithm, and times the sorting performance of each approach.

Uploaded by

공예나
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/ 7

Algorithms – Assignment 3 (Solution)

Prof. Eunwoo Kim

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

void rand_num(int arr [])


{
printf("Before Sorting \n");
for (int i = 0; i < MAX_SIZE; i++)
{
arr[i] = rand() % 9;
printf("%d ", arr[i]);
}
printf("\n");

// Bubble sort
void bubble_sort(int arr[])
{
int i, j, temp;

for (i = 0; i < MAX_SIZE - 1; i++) {


for (j = 0; j < MAX_SIZE-1-i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = 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;

for (i = 1; i < MAX_SIZE ; i++) {


key = arr[i];
for (j = i - 1; j >= 0; j--)
{
if (key < arr[j])
{
arr[j+1] = arr[j];
}
else
{
break;
}
arr[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;

while (i <= mid && j <= right) {


if (arr[i] <= arr[j])
sort_arr[k++] = arr[i++];
else
sort_arr[k++] = arr[j++];
}

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

void merge_sort(int arr[], int left, int right)


{
int mid;
if (left < right) {
mid = (left + right) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
Merge(arr, left, mid, right);
}

// Quick Sort
int Partition(int arr[], int left, int right)
{
int pivot = arr[left];
int low = left + 1;
int high = right;

while (low <= high)


{
while (low <= right && pivot >= arr[low])
low++;
while (high >= (left + 1) && pivot <= arr[high])
high--;
if (low <= high)
SWAP(arr, low, high);

}
SWAP(arr, left, high);
return high;
}

void QuickSort(int arr[], int left, int right)


{
if (left <= right)
{
int pivot = Partition(arr, left, right);
QuickSort(arr, left, pivot - 1);
QuickSort(arr, pivot + 1, right);
}
}

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;

for (int i = 0; i < MAX_SIZE; i++)


if (arr[i] > max) max = arr[i];
while (max / exp > 0) {

int bucket[MAX_SIZE] = { 0, };
for (int i = 0; i < MAX_SIZE; i++)
bucket[arr[i] / exp % 10]++;

for (int i = 1; i < 10; i++)


bucket[i] += bucket[i - 1];

for (int i = MAX_SIZE - 1; i >= 0; i--)


radix[--bucket[arr[i] / exp % 10]] = arr[i];

for (int i = 0; i < MAX_SIZE; i++)


arr[i] = radix[i];

exp *= 10;
}

void Bucket_sort(int arr[])


{
int* temp = (int*)malloc(sizeof(int)*MAX_SIZE);

for (int i = 0; i < MAX_SIZE; i++)


{
temp[i] = 0;
}

for (int i = 0; i < MAX_SIZE; i++)


temp[arr[i]]++;

for (int i = 0, j=0; i < MAX_SIZE; i++)


{
for (; temp[i] > 0; temp[i]--)
arr[j++] = i;
}
}

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

QueryPerformanceFrequency(&frequency); //microsecond timer


QueryPerformanceCounter(&start);
bubble_sort(rand_arr); //sort
QueryPerformanceCounter(&end);

printf("After Bubble Sort \n");

for (int i = 0; i < MAX_SIZE; i++)


{
printf("%d ", rand_arr[i]);
}
printf("\n");

res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;


printf("time %f\n", res);
printf("\n");

//Insertion Sort
rand_num(rand_arr);
QueryPerformanceFrequency(&frequency); //microsecond
QueryPerformanceCounter(&start);
insertion_sort(rand_arr); // sort
QueryPerformanceCounter(&end);

printf("After Insertion Sort \n");


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

res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;


printf("time %f\n", res);
printf("\n");

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

printf("After Merge Sort \n");


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

res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;

printf("time %f\n", res);


printf("\n");

//Quick Sort
rand_num(rand_arr);

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
QuickSort(rand_arr, 0, MAX_SIZE - 1); //sort
QueryPerformanceCounter(&end);

printf("After Quick Sort \n");


for (int i = 0; i < MAX_SIZE; i++)
{
printf("%d ", rand_arr[i]);
}
printf("\n");
res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;

printf("time %f\n", res);


printf("\n");

//Radix Sort
rand_num(rand_arr);

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
Radix_Sort(rand_arr); //sort
QueryPerformanceCounter(&end);

printf("After Radix Sort \n");


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

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;

printf("time %f\n", res);


printf("\n");

//Buctet Sort
rand_num(rand_arr);

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start); //microsecond
Bucket_sort(rand_arr); //sort
QueryPerformanceCounter(&end);

printf("After Bucket Sort \n");


for (int i = 0; i < MAX_SIZE; i++)
{
printf("%d ", rand_arr[i]);
}
printf("\n");
res = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart;

printf("time %f\n", res);


printf("\n");
}

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)

You might also like