Skip to content

Commit 37dede4

Browse files
Merge pull request #587 from shellhub/feature1
[fix] insertion and selection sort
2 parents d71e601 + 10d006c commit 37dede4

File tree

2 files changed

+77
-74
lines changed

2 files changed

+77
-74
lines changed

sorting/insertion_sort.c

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
// sorting of array list using insertion sort
1+
/**
2+
* @file
3+
* @brief [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort)
4+
* algorithm implementation.
5+
*/
6+
#include <assert.h>
27
#include <stdio.h>
38
#include <stdlib.h>
9+
#include <time.h>
410

5-
/*Displays the array, passed to this method*/
6-
void display(int *arr, int n)
7-
{
8-
int i;
9-
for (i = 0; i < n; i++)
10-
{
11-
printf("%d ", arr[i]);
12-
}
13-
printf("\n");
14-
}
15-
16-
/*This is where the sorting of the array takes place
17-
arr[] --- Array to be sorted
18-
size --- Array Size
11+
/**
12+
* Insertion sort algorithm implements
13+
* @param arr array to be sorted
14+
* @param size size of array
1915
*/
2016
void insertionSort(int *arr, int size)
2117
{
22-
int i, j, key;
23-
for (i = 0; i < size; i++)
18+
for (int i = 1; i < size; i++)
2419
{
25-
j = i - 1;
26-
key = arr[i];
20+
int j = i - 1;
21+
int key = arr[i];
2722
/* Move all elements greater than key to one position */
2823
while (j >= 0 && key < arr[j])
2924
{
@@ -35,28 +30,30 @@ void insertionSort(int *arr, int size)
3530
}
3631
}
3732

38-
int main(int argc, const char *argv[])
33+
/** Test function
34+
* @returns None
35+
*/
36+
static void test()
3937
{
40-
int n;
41-
printf("Enter size of array:\n");
42-
scanf("%d", &n); // E.g. 8
38+
const int size = rand() % 500; /* random array size */
39+
int *arr = (int *)calloc(size, sizeof(int));
4340

44-
printf("Enter the elements of the array\n");
45-
int i;
46-
int *arr = (int *)malloc(n * sizeof(int));
47-
for (i = 0; i < n; i++)
41+
/* generate size random numbers from -50 to 49 */
42+
for (int i = 0; i < size; i++)
4843
{
49-
scanf("%d", &arr[i]);
44+
arr[i] = (rand() % 100) - 50; /* signed random numbers */
45+
}
46+
insertionSort(arr, size);
47+
for (int i = 0; i < size - 1; ++i)
48+
{
49+
assert(arr[i] <= arr[i + 1]);
5050
}
51-
52-
printf("Original array: ");
53-
display(arr, n);
54-
55-
insertionSort(arr, n);
56-
57-
printf("Sorted array: ");
58-
display(arr, n);
59-
6051
free(arr);
52+
}
53+
int main(int argc, const char *argv[])
54+
{
55+
/* Intializes random number generator */
56+
srand(time(NULL));
57+
test();
6158
return 0;
6259
}

sorting/selection_sort.c

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
// sorting of array list using selection sort
1+
/**
2+
* @file
3+
* @brief [Selection sort](https://en.wikipedia.org/wiki/Selection_sort)
4+
* algorithm implementation.
5+
*/
6+
#include <assert.h>
27
#include <stdio.h>
38
#include <stdlib.h>
9+
#include <time.h>
410

5-
/*Displays the array, passed to this method*/
6-
void display(int *arr, int n)
7-
{
8-
int i;
9-
for (i = 0; i < n; i++)
10-
{
11-
printf("%d ", arr[i]);
12-
}
13-
14-
printf("\n");
15-
}
16-
17-
/*Swap function to swap two values*/
11+
/**
12+
* Swapped two numbers using pointer
13+
* @param first first pointer of first number
14+
* @param second second pointer of second number
15+
*/
1816
void swap(int *first, int *second)
1917
{
2018
int temp = *first;
2119
*first = *second;
2220
*second = temp;
2321
}
2422

25-
/*This is where the sorting of the array takes place
26-
arr[] --- Array to be sorted
27-
size --- Array Size
23+
/**
24+
* Selection sort algorithm implements
25+
* @param arr array to be sorted
26+
* @param size size of array
2827
*/
2928
void selectionSort(int *arr, int size)
3029
{
31-
for (int i = 0; i < size; i++)
30+
for (int i = 0; i < size - 1; i++)
3231
{
3332
int min_index = i;
3433
for (int j = i + 1; j < size; j++)
@@ -38,32 +37,39 @@ void selectionSort(int *arr, int size)
3837
min_index = j;
3938
}
4039
}
41-
swap(&arr[i], &arr[min_index]);
40+
if (min_index != i)
41+
{
42+
swap(arr + i, arr + min_index);
43+
}
4244
}
4345
}
4446

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

51-
printf("Enter the elements of the array\n");
52-
int i;
53-
int *arr = (int *)malloc(n * sizeof(int));
54-
for (i = 0; i < n; i++)
55+
/* generate size random numbers from -50 to 49 */
56+
for (int i = 0; i < size; i++)
5557
{
56-
scanf("%d", &arr[i]);
58+
arr[i] = (rand() % 100) - 50; /* signed random numbers */
59+
}
60+
selectionSort(arr, size);
61+
for (int i = 0; i < size - 1; ++i)
62+
{
63+
assert(arr[i] <= arr[i + 1]);
5764
}
58-
59-
printf("Original array: ");
60-
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
61-
62-
selectionSort(arr, n);
63-
64-
printf("Sorted array: ");
65-
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
66-
6765
free(arr);
66+
}
67+
68+
/** Driver Code */
69+
int main(int argc, const char *argv[])
70+
{
71+
/* Intializes random number generator */
72+
srand(time(NULL));
73+
test();
6874
return 0;
6975
}

0 commit comments

Comments
 (0)