Skip to content

Commit c8a003e

Browse files
committed
sorting_algorithms
1 parent 78f6c3f commit c8a003e

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

0-O

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(n)
2+
O(n^2)
3+
O(n^2)

2-O

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(n^2)
2+
O(n^2)
3+
O(n^2)

2-main.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "sort.h"
4+
5+
/**
6+
* main - Entry point
7+
*
8+
* Return: Always 0
9+
*/
10+
int main(void)
11+
{
12+
int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7};
13+
size_t n = sizeof(array) / sizeof(array[0]);
14+
15+
print_array(array, n);
16+
printf("\n");
17+
selection_sort(array, n);
18+
printf("\n");
19+
print_array(array, n);
20+
return (0);
21+
}

2-selection_sort.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "sort.h"
2+
3+
/**
4+
* selection_sort - Calls function
5+
* @array: Array to be sorted
6+
* @size: Size of array given
7+
* Descrtiption: Function that sorts an array using the Selection
8+
* sort algotrithm
9+
* Return: 0
10+
*/
11+
void selection_sort(int *array, size_t size)
12+
{
13+
unsigned int i, j, min_idx;
14+
15+
if (!array)
16+
return;
17+
18+
for (i = 0; i < size - 1; i++)
19+
{
20+
min_idx = i;
21+
for (j = i + 1; j < size; j++)
22+
if (array[j] < array[min_idx])
23+
min_idx = j;
24+
25+
if (i != min_idx)
26+
{
27+
swap_func(&array[min_idx], &array[i]);
28+
print_array(array, size);
29+
}
30+
}
31+
}
32+
33+
/**
34+
* swap_func - Function that swaps two values
35+
*
36+
* @a: Fisrt value
37+
* @b: Second value
38+
* Return: 0
39+
*/
40+
void swap_func(int *a, int *b)
41+
{
42+
int tmp;
43+
44+
tmp = *b;
45+
*b = *a;
46+
*a = tmp;
47+
}

0-main.c renamed to main/0-main.c

File renamed without changes.

1-main.c renamed to main/1-main.c

File renamed without changes.

output/select

16.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)