Selection sort algorithm implementation.
More...
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
void | swap (int *first, int *second) |
| Swapped two numbers using pointer.
|
|
void | selectionSort (int *arr, int size) |
| Selection sort algorithm implements.
|
|
static void | test () |
| Test function.
|
|
int | main (int argc, const char *argv[]) |
| Driver Code.
|
|
Selection sort algorithm implementation.
◆ main()
int main |
( |
int |
argc, |
|
|
const char * |
argv[] |
|
) |
| |
Driver Code.
70{
71
72 srand(time(NULL));
74 return 0;
75}
static void test()
Test function.
Definition selection_sort.c:50
◆ selectionSort()
void selectionSort |
( |
int * |
arr, |
|
|
int |
size |
|
) |
| |
Selection sort algorithm implements.
- Parameters
-
arr | array to be sorted |
size | size of array |
29{
30 for (int i = 0; i < size - 1; i++)
31 {
32 int min_index = i;
33 for (int j = i + 1; j < size; j++)
34 {
35 if (arr[min_index] > arr[j])
36 {
37 min_index = j;
38 }
39 }
40 if (min_index != i)
41 {
42 swap(arr + i, arr + min_index);
43 }
44 }
45}
◆ swap()
void swap |
( |
int * |
first, |
|
|
int * |
second |
|
) |
| |
Swapped two numbers using pointer.
- Parameters
-
first | first pointer of first number |
second | second pointer of second number |
17{
18 int temp = *first;
19 *first = *second;
20 *second = temp;
21}
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Test function.
- Returns
- None
51{
52 const int size = rand() % 500;
53 int *arr = (
int *)
calloc(size,
sizeof(
int));
54
55
56 for (int i = 0; i < size; i++)
57 {
58 arr[i] = (rand() % 100) - 50;
59 }
61 for (int i = 0; i < size - 1; ++i)
62 {
63 assert(arr[i] <= arr[i + 1]);
64 }
66}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22
void selectionSort(int *arr, int size)
Selection sort algorithm implements.
Definition selection_sort.c:28