[go: up one dir, main page]

0% found this document useful (0 votes)
20 views4 pages

Sort Codes

The code implements various sorting algorithms like bubble sort, insertion sort, merge sort and quick sort. It takes input from a file, sorts the numbers using the specified algorithm and writes the output to another file. The main function reads numbers from the input file and calls the appropriate sorting function based on the second command line argument. The sorting functions are defined in separate files and include a header file for function declarations.

Uploaded by

f20201654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Sort Codes

The code implements various sorting algorithms like bubble sort, insertion sort, merge sort and quick sort. It takes input from a file, sorts the numbers using the specified algorithm and writes the output to another file. The main function reads numbers from the input file and calls the appropriate sorting function based on the second command line argument. The sorting functions are defined in separate files and include a header file for function declarations.

Uploaded by

f20201654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//main.

#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

FILE* file;
file = fopen(argv[1], "r");
int arr[50];
int i = 0;
while (fscanf(file, "%d", &arr[i]) == 1){
i++;
}

if (strcmp(argv[2], "bubble") == 0){


bubble(arr, i);
}
else if (strcmp(argv[2], "quick") == 0)
{
quick(arr, 0, i - 1);
}
else if (strcmp(argv[2], "insert") == 0)
{
insort(arr, i);
}
else if (strcmp(argv[2], "merge") == 0)
{
mergeSort(arr, 0, i - 1);
}
else{
perror("Wrong Input");
}
fclose(file);
char s[100];
scanf("%s", s);
FILE* file1;
file1 = fopen(s, "w");
for (int j = 0; j < i; j++){
fprintf(file1, "%d ", arr[j]);
}

fclose(file1);
}

//bubble.c

#include "test.h"
#include <stdio.h>
#include <stdlib.h>

void bubble(int arr[], int n) {


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

// insort.c

#include "test.h"
#include <stdio.h>
#include <stdlib.h>

void insort(int arr[], int n) {


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

//merge.c

#include "test.h"
#include <stdio.h>
#include <stdlib.h>

void merge(int arr[], int beg, int mid, int end)


{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

int LeftArray[n1], RightArray[n2]; //temporary arrays

/* copy data to temp arrays */


for (int i = 0; i < n1; i++)
LeftArray[i] = arr[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = arr[mid + 1 + j];

i = 0, /* initial index of first sub-array */


j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

while (i < n1 && j < n2)


{
if(LeftArray[i] <= RightArray[j])
{
arr[k] = LeftArray[i];
i++;
}
else
{
arr[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
arr[k] = LeftArray[i];
i++;
k++;
}

while (j<n2)
{
arr[k] = RightArray[j];
j++;
k++;
}
}

void mergeSort(int arr[], int beg, int end)


{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(arr, beg, mid);
mergeSort(arr, mid + 1, end);
merge(arr, beg, mid, end);
}
}

//quick.c

#include "test.h"
#include <stdio.h>
#include <stdlib.h>

int partition(int arr[], int low, int high)


{
int pivot = arr[high];
int i = (low - 1);
int j;
for (j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++;
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
int t = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = t;
return (i + 1);
}

void quick(int arr[], int low, int high)


{
if (low < high) {
// pi = Partition index
int pi = partition(arr, low, high);
quick(arr, low, pi - 1);
quick(arr, pi + 1, high);
}
}

//test.h

#ifndef _HW_TEST_
#define _HW_TEST_

extern void bubble(int arr[], int n);


extern void insort(int arr[], int n);
extern void quick(int arr[], int low, int high);
extern int partition(int arr[], int low, int high);
extern void mergeSort(int arr[], int beg, int end);
extern void merge(int arr[], int beg, int mid, int end) ;

#endif

//makefile

runner.out: main.c bubble.o merge.o insort.o quick.o


gcc -o runner.out main.c bubble.o merge.o insort.o quick.o

bubble.o: bubble.c test.h


gcc -c bubble.c

merge.o: merge.c test.h


gcc -c merge.c

quick.o: quick.c test.h


gcc -c quick.c

insort.o: insort.c test.h


gcc -c insort.c

clean:
rm -f *.o *.out

Array.txt

23 5 7 10 6 100 25

You might also like