-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11. Bubble Sort.js
More file actions
53 lines (48 loc) · 1.35 KB
/
11. Bubble Sort.js
File metadata and controls
53 lines (48 loc) · 1.35 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// // BUBBLE SORT
// // Compara todos os elementos do vetor, sempre trocando elementos vizinhos caso estes não estejam ordenados.
// // Não-otimizado
// function bubbleSort(arr){
// for(let i=0; i<arr.length; i++){
// for(let j=0; j<arr.length; j++){
// if(arr[j] > arr[j+1]){
// let temp = arr[j];
// arr[j] = arr[j+1];
// arr[j+1] = temp;
// }
// }
// }
// return arr;
// }
// bubbleSort([37, 45, 29, 8]);
// // Semi-otimizado
// function bubbleSort(arr){
// for(let i=arr.length; i>0; i--){
// for(let j=0; j<i-1; j++){
// if(arr[j] > arr[j+1]){
// let temp = arr[j];
// arr[j] = arr[j+1];
// arr[j+1] = temp;
// }
// }
// }
// return arr;
// }
// bubbleSort([37, 45, 29, 8]);
// // Otimizado com o noSwaps [O(n)]
// function bubbleSort(arr){
// let noSwaps;
// for(let i=arr.length; i>0; i--){
// noSwaps = true;
// for(let j=0; j<i-1; j++){
// if(arr[j] > arr[j+1]){
// let temp = arr[j];
// arr[j] = arr[j+1];
// arr[j+1] = temp;
// noSwaps = false;
// }
// }
// if(noSwaps) break;
// }
// return arr;
// }
// bubbleSort([37, 45, 29, 8]);