-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
36 lines (31 loc) · 1.32 KB
/
BubbleSort.cpp
File metadata and controls
36 lines (31 loc) · 1.32 KB
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
#include <iostream>
void bubbleSort(int array[], int size); // function declaration
int main()
{
int array[] = { 10, 1, 9, 2, 8, 3, 7, 4, 6, 5}; // array of ints
int size = sizeof(array)/sizeof(array[0]); // calculates number of elements in array
bubbleSort(array, size); // calls the function and passes the array to the function to get sorted.
//The sorted array in the other function carries over to the main function because the array is passed by reference
//(its a pointer, so change the pointer you chance the original)
for(int element : array)
{
std::cout << element << " ";
}
return 0;
}
void bubbleSort(int array[], int size) //bubble sort, O(n^2) time complexity because it goes through n elements and compares them to n elements
{
int temp;
for(int i = 0; i < size - 1; i++)
{
for(int j = 0; j < size - i - 1; j++) //adding - i is not necessary but it makes the algorithm more efficient because the last i elements are already sorted
{
if(array[j] > array[j + 1]) // compares the two elements and sorts in acending order, if you want decending change the > to <
{
temp = array[j]; // swaps the two elements
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}