From bc1c15e24fe85de363b61d2e28218bb042e45174 Mon Sep 17 00:00:00 2001 From: sungbin <21700613@handong.edu> Date: Thu, 8 Nov 2018 16:04:04 +0900 Subject: [PATCH 1/4] Change SelectionSort.c format --- sorting/SelectionSort.c | 149 +++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 87 deletions(-) diff --git a/sorting/SelectionSort.c b/sorting/SelectionSort.c index 076fad09..db269c69 100644 --- a/sorting/SelectionSort.c +++ b/sorting/SelectionSort.c @@ -1,89 +1,64 @@ //sorting of linked list using selection sort -#include -struct node -{ - int info; - struct node *link; -}; -struct node *start=NULL; -//func to create node -struct node *createnode() -{ - struct node *p; - p=(struct node*)malloc(sizeof(struct node)); - return(p); +#include + +/*Displays the array, passed to this method*/ +void display(int arr[], int n){ + + int i; + for(i = 0; i < n; i++){ + printf("%d ", arr[i]); + } + + printf("\n"); + } -//program to insert at begining -void insert() -{struct node *t; - t=createnode(); - printf("\nenter the value to insert"); - scanf("%d",&t->info); - if(start==NULL) - {start=t; - } - else - {strutc node *p; - p=start; - t->link=p; - start=t; - } - //program to sort the linked list using selection sort - void sort() - { - struct node *p,*t; - t=start; - int tmp; - for(t=start;t->link!=NULL;t=t->link) - { - for(p=t->link;p!=NULL;p=p->link) - { - if(t->info>p->info) - tmp=t->info; - t->info=p->info; - p->info=tmp; - } - } - //program to view sorted list - void viewlist() - { - struct node *p; - if(start==NULL) - { - printf("\nlist is empty"); - } - else - { - p=start; - while(p!=NULL) - { - printf("%d",p->info); - p=p->link; - } - } - int main() - { - int n; - whhile(1) - { - printf("\n1.insert value at beg"); - printf("\n2.sort the list"); - printf("\n3.view value"); - printf("\nenter your choice"); - scanf("%d",&n); - switch(n) - {case 1: - insert(); - break; - case 2: - sort(); - break; - case 3: - viewlist(); - break; - default: - printf("\ninvalid choice"); - } - } - return(0); - } + +/*Swap function to swap two values*/ +void swap(int *first, int *second){ + + int temp = *first; + *first = *second; + *second = temp; + +} + +/*This is where the sorting of the array takes place + arr[] --- Array to be sorted + size --- Array Size + */ +void selectionSort(int arr[], int size){ + + for(int i=0; i arr[j]) { + min_index = j; + } + } + swap(&arr[i], &arr[min_index]); + } +} + +int main(int argc, const char * argv[]) { + int n; + printf("Enter size of array:\n"); + scanf("%d", &n); // E.g. 8 + + printf("Enter the elements of the array\n"); + int i; + int arr[n]; + for(i = 0; i < n; i++){ + scanf("%d", &arr[i] ); + } + + printf("Original array: "); + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + + selectionSort(arr, n); + + printf("Sorted array: "); + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + + return 0; +} + From edf400739c3b52ae5bbb0da5d8da9e67ffee6a74 Mon Sep 17 00:00:00 2001 From: sungbin <21700613@handong.edu> Date: Thu, 8 Nov 2018 16:22:51 +0900 Subject: [PATCH 2/4] Change BubbleSort.c format --- sorting/BubbleSort.c | 82 ++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/sorting/BubbleSort.c b/sorting/BubbleSort.c index 15827f07..e54cb4e6 100644 --- a/sorting/BubbleSort.c +++ b/sorting/BubbleSort.c @@ -1,40 +1,62 @@ +//sorting of linked list using selection sort #include -#include - -int main(){ - - int* ARRAY=NULL; - int ContinueFilling=1; //This is to know if we should continue filling our array - int ARRAY_LENGTH=0,isSorted=0,i,TEMPORARY_ELEMENT; - - //This code part is for filling our array - while(ContinueFilling){ - printf("Enter the value number %d \n",ARRAY_LENGTH+1); - ARRAY=(int *)realloc(ARRAY,sizeof(int)*(ARRAY_LENGTH)); - scanf("%d",&ARRAY[ARRAY_LENGTH]); - ARRAY_LENGTH+=1; - printf("would you enter an other value (1:Continue/0:Sort the actual array)?\n"); - scanf("%d",&ContinueFilling); +/*Displays the array, passed to this method*/ +void display(int arr[], int n){ + + int i; + for(i = 0; i < n; i++){ + printf("%d ", arr[i]); } + + printf("\n"); + +} - //Then we sort it using Bubble Sort.. +/*Swap function to swap two values*/ +void swap(int *first, int *second){ + + int temp = *first; + *first = *second; + *second = temp; + +} - while(!isSorted){ //While our array's not sorted - isSorted=1; //we suppose that it's sorted - for(i=0;iARRAY[i+1]){ // if the two elements aren't sorted - isSorted=0; //it means that the array is not sorted - TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT - ARRAY[i]=ARRAY[i+1]; - ARRAY[i+1]=TEMPORARY_ELEMENT; +/*This is where the sorting of the array takes place + arr[] --- Array to be sorted + size --- Array Size + */ +void bubbleSort(int arr[], int size){ + + for(int i=0; iarr[j+1]) { + swap(&arr[j], &arr[j+1]); } } } - //And we display it - for(i=0;i Date: Thu, 8 Nov 2018 16:37:38 +0900 Subject: [PATCH 3/4] Change InsertionSort.c format --- sorting/BubbleSort.c | 2 +- sorting/InsertionSort.c | 80 +++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/sorting/BubbleSort.c b/sorting/BubbleSort.c index e54cb4e6..a92b4892 100644 --- a/sorting/BubbleSort.c +++ b/sorting/BubbleSort.c @@ -1,4 +1,4 @@ -//sorting of linked list using selection sort +//sorting of linked list using bubble sort #include /*Displays the array, passed to this method*/ diff --git a/sorting/InsertionSort.c b/sorting/InsertionSort.c index 9e5da501..613e70b8 100644 --- a/sorting/InsertionSort.c +++ b/sorting/InsertionSort.c @@ -1,35 +1,53 @@ +//sorting of linked list using insertion sort #include -#incude -#define MAX 20 -//i and j act as counters -//arraySort is the array that is to be sorted -//elmtToInsert will be the element that we will be trying to move to its correct index in the current iteration - -int main() -{ - int i, elmtToInsert , j , arraySort[MAX] = {0}; - - for(i = 1 ; i < MAX ; i++) //This array is being sorted in the ascending order. - { - elmtToInsert = arraySort[i]; //Pick up the ith indexed element of the array. It will be the elmtToInsert. - j = i - 1 ; - - while(j >= 0 && elmtToInsert < arraySort[j]) /*compare it with each (i-1)th, (i-2)th... max 0th element, till the correct - position of the elmtToInsert, where it is finally greater than the element just - before it, is found */ - { - // You'll enter the loop if the elmtToInsert is less than the element just before it. - - arraySort[j+1] = arraySort[j]; //shift the current element one place forward to create room for insertion of the elmtToInsert - j--; - } - //when we exit the loop, j+1 will be the index of the correct position of the elmtToInsert - - arraySort[j+1] = elmtToInsert ; //'insert' the elmtToInsert into its correct position - - } +/*Displays the array, passed to this method*/ +void display(int arr[], int n){ + + int i; + for(i = 0; i < n; i++){ + printf("%d ", arr[i]); + } + + printf("\n"); - - return EXIT_SUCCESS; } + +/*This is where the sorting of the array takes place + arr[] --- Array to be sorted + size --- Array Size + */ +void insertionSort(int arr[], int size){ + int j,temp,i; + for(i=0; i= 0 && temp < arr[j]) { + arr[j+1] = arr[j]; + arr[j] = temp; + } + } +} + +int main(int argc, const char * argv[]) { + int n; + printf("Enter size of array:\n"); + scanf("%d", &n); // E.g. 8 + + printf("Enter the elements of the array\n"); + int i; + int arr[n]; + for(i = 0; i < n; i++){ + scanf("%d", &arr[i] ); + } + + printf("Original array: "); + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + + insertionSort(arr, n); + + printf("Sorted array: "); + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + + return 0; +} + From a498bf1de91974a0dafb845e205bfff130eea503 Mon Sep 17 00:00:00 2001 From: sungbin <21700613@handong.edu> Date: Tue, 13 Nov 2018 13:42:00 +0900 Subject: [PATCH 4/4] Better ome comments --- sorting/BubbleSort.c | 2 +- sorting/InsertionSort.c | 2 +- sorting/SelectionSort.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sorting/BubbleSort.c b/sorting/BubbleSort.c index a92b4892..0b6a1879 100644 --- a/sorting/BubbleSort.c +++ b/sorting/BubbleSort.c @@ -1,4 +1,4 @@ -//sorting of linked list using bubble sort +//sorting of array list using bubble sort #include /*Displays the array, passed to this method*/ diff --git a/sorting/InsertionSort.c b/sorting/InsertionSort.c index 613e70b8..961ddebb 100644 --- a/sorting/InsertionSort.c +++ b/sorting/InsertionSort.c @@ -1,4 +1,4 @@ -//sorting of linked list using insertion sort +//sorting of array list using insertion sort #include /*Displays the array, passed to this method*/ diff --git a/sorting/SelectionSort.c b/sorting/SelectionSort.c index db269c69..cff71b4d 100644 --- a/sorting/SelectionSort.c +++ b/sorting/SelectionSort.c @@ -1,4 +1,4 @@ -//sorting of linked list using selection sort +//sorting of array list using selection sort #include /*Displays the array, passed to this method*/