-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
44 lines (36 loc) · 982 Bytes
/
BubbleSort.cpp
File metadata and controls
44 lines (36 loc) · 982 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
void swap(int vector[], int i, int j){
int temp = vector[i];
vector[i] = vector[j];
vector[j] = temp;
}
void bubbleSort(int vector[], int leftIndex, int rightIndex){
for (int i = rightIndex; i > leftIndex; i--)
for (int j = leftIndex; j < i; j++)
if (vector[j] > vector[j+1])
swap(vector, j, j+1);
}
void bubbleSort(int vector[], int n){
bubbleSort(vector, 0, n-1);
}
void printArray(int arr[], int n){
cout << "[";
int i;
for (i = 0; i < n; i++)
if (i == n-1)
cout << arr[i];
else
cout << arr[i] << " ";
cout << "]" << endl;
}
int main(){
int array[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(array) / sizeof(int);
cout << "Array antes do sort: ";
printArray(array, n);
bubbleSort(array, 0, 3);
cout << "Array depois do sort: ";
printArray(array, n);
}