Skip to content

Commit 44ef511

Browse files
committed
sorting_algorithms
0 parents  commit 44ef511

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed

0-bubble_sort.c

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

0-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+
bubble_sort(array, n);
18+
printf("\n");
19+
print_array(array, n);
20+
return (0);
21+
}

LICENCE

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sorting_algorithms

bubble

16.6 KB
Binary file not shown.

print_array.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include "sort.h"
3+
4+
/**
5+
* print_array - Prints an array of integers
6+
*
7+
* @array: The array to be printed
8+
* @size: Number of elements in @array
9+
*/
10+
void print_array(const int *array, size_t size)
11+
{
12+
size_t i;
13+
14+
i = 0;
15+
while (array && i < size)
16+
{
17+
if (i > 0)
18+
printf(", ");
19+
printf("%d", array[i]);
20+
++i;
21+
}
22+
printf("\n");
23+
}

sort.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef SORT_H
2+
#define SORT_H
3+
4+
5+
#include <stdio.h>
6+
#include "sort.h"
7+
8+
/**
9+
* enum bool - Enumeration of Boolean values.
10+
* @false: Equals 0.
11+
* @true: Equals 1.
12+
*/
13+
typedef enum bool
14+
{
15+
false = 0,
16+
true
17+
} bool;
18+
19+
/**
20+
* struct listint_s - Doubly linked list node
21+
*
22+
* @n: Integer stored in the node
23+
* @prev: Pointer to the previous element of the list
24+
* @next: Pointer to the next element of the list
25+
*/
26+
typedef struct listint_s
27+
{
28+
const int n;
29+
struct listint_s *prev;
30+
struct listint_s *next;
31+
} listint_t;
32+
33+
void bubble_sort(int *array, size_t size);
34+
void insertion_sort_list(listint_t **list);
35+
void quick_sort(int *array, size_t size);
36+
void selection_sort(int *array, size_t size);
37+
size_t partition(int array[], size_t size);
38+
void print_sort(int array[], size_t size, int init);
39+
void shell_sort(int *array, size_t size);
40+
void cocktail_sort_list(listint_t **list);
41+
void counting_sort(int *array, size_t size);
42+
void merge_sort(int *array, size_t size);
43+
void heap_sort(int *array, size_t size);
44+
void radix_sort(int *array, size_t size);
45+
void bitonic_sort(int *array, size_t size);
46+
void quick_sort_hoare(int *array, size_t size);
47+
void print_list(const listint_t *list);
48+
void print_array(const int *array, size_t size);
49+
void swap_func(int *a, int *b);
50+
void heapify_func(int *array, int end, int start, size_t size);
51+
52+
#endif

0 commit comments

Comments
 (0)