-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.c
58 lines (49 loc) · 1.36 KB
/
BubbleSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
/**
* Sorts an array using the bubble sort algorithm.
*
* Time Complexity: O(n^2), where n is the number of elements in the array.
* Bubble sort iteratively compares adjacent elements and swaps them if they are in the wrong order until the entire array is sorted.
*
* @param array : The array to be sorted.
* @param size : The size of the array.
*/
void bubbleSort(signed int array[], unsigned int size);
/**
* Swaps two integers using bitwise XOR operation.
*
* @param numberOne : Pointer to the first integer.
* @param numberTwo : Pointer to the second integer.
*/
void swap(signed int *numberOne, signed int *numberTwo);
int main()
{
return 0;
}
void bubbleSort(signed int array[], unsigned int size)
{
unsigned int l_counter = 0;
unsigned int l_iterator = 0;
unsigned int flag = 1;
for(l_counter = 0; l_counter < size - 1; ++l_counter)
{
for(l_iterator = 0; l_iterator < size - l_counter -1; ++l_iterator)
{
if(array[l_iterator] > array[l_iterator + 1])
{
flag = 0;
swap(&array[l_iterator], &array[l_iterator + 1]);
}
if(flag)
{
return;
}
}
}
}
void swap(signed int *numberOne, signed int *numberTwo)
{
(*numberOne) = (*numberOne) ^ (*numberTwo);
(*numberTwo) = (*numberOne) ^ (*numberTwo);
(*numberOne) = (*numberOne) ^ (*numberTwo);
}