Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hey #11

Open
wants to merge 5 commits into
base: add-a-list
Choose a base branch
from
Open

hey #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ This is a demo repository for I/O
* Hello
* Ola
* How are you

*Prateek*

So do time analysis on 4 sorting functions
146 changes: 146 additions & 0 deletions a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include <stdlib.h>
#include <stdio.h>
#include "a.h"

//Swap function
void swap(long int *a, long int *b) {
long int t = *a;
*a = *b;
*b = t;
}

// Merge Sort
void mergeA(long int a[], long int p, long int q, long int r, long int *num_cmp)
{

long int n1 = q - p + 1;
long int n2 = r - q;

long int L[n1], M[n2];

for (long int i = 0; i < n1; i++)
L[i] = a[p + i];
for (long int j = 0; j < n2; j++)
M[j] = a[q + 1 + j];

long int i, j, k;
i = 0;
j = 0;
k = p;

while (i < n1 && j < n2)
{
*num_cmp = *num_cmp + 1;

if (L[i] <= M[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = M[j];
j++;
}
k++;
}

while (i < n1)
{
a[k] = L[i];
i++;
k++;
}

while (j < n2)
{
a[k] = M[j];
j++;
k++;
}
}

void mergeSort(long int a[], long int l, long int r, long int *num_cmp)
{
if (l < r)
{
long int m = l + (r - l) / 2;

mergeSort(a, l, m, num_cmp);
mergeSort(a, m + 1, r, num_cmp);

mergeA(a, l, m, r, num_cmp);
}
}

// Bubble Sort
void bubbleSort(long int a[], long int n, long int *num_cmp)
{
for (long int i = 0; i < n - 1; i++)
{
for (long int j = 0; j < n - 1 - i; j++)
{
// printf("%li %li\n", a[j], j);

*num_cmp = *num_cmp + 1;
if (a[j] > a[j + 1])
{
swap(&a[j], &a[j + 1]);
}
}
}

}

// Select Sort
void selectionSort(long int a[], long int n, long int *num_cmp)
{
long int i, j, min;

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

min = i;

for (j = i + 1; j < n; j++)
*num_cmp = *num_cmp + 1;
if (a[j] < a[min])
{
min = j;

}


swap(&a[min], &a[i]);
}
}

// Quick Sort

int partition(long int a[], long int l, long int h, long int *num_cmp) {

long int piv = a[h];
long int i = (l - 1);

for (long int j = l; j < h; j++) {
*num_cmp = *num_cmp + 1;
if (a[j] <= piv) {
i++;
swap(&a[i], &a[j]);
}
}

swap(&a[i + 1], &a[h]);
return (i + 1);
}

void quickSort(long int a[], long int l, long int h, long int *num_cmp) {
if (l < h) {

long int pi = partition(a, l, h, num_cmp);

quickSort(a, l, pi - 1, num_cmp);

quickSort(a, pi + 1, h, num_cmp);
}
}
7 changes: 7 additions & 0 deletions a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <stdlib.h>

void bubbleSort (long int a[], long int n, long int* c);
void quickSort (long int a[], long int l, long int h, long int* c);
void selectionSort (long int a[], long int n, long int *c);
void mergeSort (long int a[], long int l, long int r, long int* c);
119 changes: 119 additions & 0 deletions client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "a.h"
#include <time.h>

void main()
{
FILE *t1, *t2, *t3, *t4;
FILE *c1, *c2, *c3, *c4;

//Create output files
t1 = fopen("bs1.dat", "w");
t2 = fopen("ss1.dat", "w");
t3 = fopen("ms1.dat", "w");
t4 = fopen("qs1.dat", "w");
c1 = fopen("bs2.dat", "w");
c2 = fopen("ss2.dat", "w");
c3 = fopen("ms2.dat", "w");
c4 = fopen("qs2.dat", "w");

//Add color option - to differentiate when graphing with Xgraph
fprintf(t1,"color = 2\n");
fprintf(c1,"color = 2\n");

fprintf(t2,"color = 4\n");
fprintf(c2,"color = 4\n");

fprintf(t3,"color = 7\n");
fprintf(c3,"color = 7\n");

fprintf(t4,"color = 3\n");
fprintf(c4,"color = 3\n");

//Initi n to 100k
long int n = 100000;
//Init variable to store number of comparisions
long int cmp;

// 1050000

while (n != 1050000)
{
//Allocate required memory
long int *a = malloc (sizeof (long int) * n);
long int *b = malloc (sizeof (long int) * n);
long int *c = malloc (sizeof (long int) * n);
long int *d = malloc (sizeof (long int) * n);

//Generate random values for the allocated array
long int rand_num = 0;
for (long int i = 0; i < n; i++)
{
rand_num = rand() % n + 1;
a[i] = rand_num;
b[i] = rand_num;
c[i] = rand_num;
d[i] = rand_num;
}
printf("%li\n", n);

//Using clock_t to keep time
clock_t start, end;

//Bubble sort
cmp = 0;
start = clock();
bubbleSort(a, n - 1, &cmp);
end = clock();
//Print data acquired in the respective file
fprintf(t1, "%li\t%lf\n", n, ((double)(end - start)));
fprintf(c1, "%li\t%li\n", n, cmp);
printf("*\n");

//Selection sort
cmp = 0;
start = clock();
selectionSort(b, n - 1, &cmp);
end = clock();
fprintf(t2, "%li\t%lf\n", n, ((double)(end - start)));
fprintf(c2, "%li\t%li\n", n, cmp);
printf("*\n");

//Merge Sort
cmp = 0;
start = clock();
mergeSort(c, 0, n - 1, &cmp);
end = clock();
fprintf(t3, "%li\t%lf\n", n, ((double)(end - start)));
fprintf(c3, "%li\t%li\n", n, cmp);
printf("*\n");

//Quick Sort
cmp = 0;
start = clock();
quickSort(d, 0, n - 1, &cmp);
end = clock();
fprintf(t4, "%li\t%lf\n", n, ((double)(end - start)));
fprintf(c4, "%li\t%li\n", n, cmp);
printf("*\n");

//Append by 50k each time
n += 50000;

//Free allocated memory
free(a);
free(b);
free(c);
free(d);
}

//Cleaning up after
//Close and save the output files associated with each function
fclose(t1);
fclose(t2);
fclose(t3);
fclose(t4);
fclose(c1);
fclose(c2);
fclose(c3);
fclose(c4);
}
Binary file added sizeVSnumcomp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sizeVStime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.