Skip to content

Commit

Permalink
* fix docs
Browse files Browse the repository at this point in the history
* fix insertion sort and selection sort
  • Loading branch information
realDuYuanChao committed Jul 31, 2020
1 parent ff2e7a3 commit 10d006c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 74 deletions.
71 changes: 34 additions & 37 deletions sorting/insertion_sort.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
// sorting of array list using insertion sort
/**
* @file
* @brief [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort)
* algorithm implementation.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*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");
}

/*This is where the sorting of the array takes place
arr[] --- Array to be sorted
size --- Array Size
/**
* Insertion sort algorithm implements
* @param arr array to be sorted
* @param size size of array
*/
void insertionSort(int *arr, int size)
{
int i, j, key;
for (i = 0; i < size; i++)
for (int i = 1; i < size; i++)
{
j = i - 1;
key = arr[i];
int j = i - 1;
int key = arr[i];
/* Move all elements greater than key to one position */
while (j >= 0 && key < arr[j])
{
Expand All @@ -35,28 +30,30 @@ void insertionSort(int *arr, int size)
}
}

int main(int argc, const char *argv[])
/** Test function
* @returns None
*/
static void test()
{
int n;
printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8
const int size = rand() % 500; /* random array size */
int *arr = (int *)calloc(size, sizeof(int));

printf("Enter the elements of the array\n");
int i;
int *arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
/* generate size random numbers from -50 to 49 */
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
arr[i] = (rand() % 100) - 50; /* signed random numbers */
}
insertionSort(arr, size);
for (int i = 0; i < size - 1; ++i)
{
assert(arr[i] <= arr[i + 1]);
}

printf("Original array: ");
display(arr, n);

insertionSort(arr, n);

printf("Sorted array: ");
display(arr, n);

free(arr);
}
int main(int argc, const char *argv[])
{
/* Intializes random number generator */
srand(time(NULL));
test();
return 0;
}
80 changes: 43 additions & 37 deletions sorting/selection_sort.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
// sorting of array list using selection sort
/**
* @file
* @brief [Selection sort](https://en.wikipedia.org/wiki/Selection_sort)
* algorithm implementation.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*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");
}

/*Swap function to swap two values*/
/**
* Swapped two numbers using pointer
* @param first first pointer of first number
* @param second second pointer of second number
*/
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
/**
* Selection sort algorithm implements
* @param arr array to be sorted
* @param size size of array
*/
void selectionSort(int *arr, int size)
{
for (int i = 0; i < size; i++)
for (int i = 0; i < size - 1; i++)
{
int min_index = i;
for (int j = i + 1; j < size; j++)
Expand All @@ -38,32 +37,39 @@ void selectionSort(int *arr, int size)
min_index = j;
}
}
swap(&arr[i], &arr[min_index]);
if (min_index != i)
{
swap(arr + i, arr + min_index);
}
}
}

int main(int argc, const char *argv[])
/** Test function
* @returns None
*/
static void test()
{
int n;
printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8
const int size = rand() % 500; /* random array size */
int *arr = (int *)calloc(size, sizeof(int));

printf("Enter the elements of the array\n");
int i;
int *arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
/* generate size random numbers from -50 to 49 */
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
arr[i] = (rand() % 100) - 50; /* signed random numbers */
}
selectionSort(arr, size);
for (int i = 0; i < size - 1; ++i)
{
assert(arr[i] <= arr[i + 1]);
}

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

free(arr);
}

/** Driver Code */
int main(int argc, const char *argv[])
{
/* Intializes random number generator */
srand(time(NULL));
test();
return 0;
}

0 comments on commit 10d006c

Please sign in to comment.