You are on page 1of 2

1: #include <iostream>

2: #include <stdlib.h>
3: #define N 6
4: //sortare in ordine crescatoare
5:
6: using namespace std;
7:
8: int vInitial[N]={7, -3, -1, 9, -6, 8}, v[N];
9:
10: void bubbleSort1(){
11: bool sortat;
12: int i, temp;
13: do{
14: sortat = true;
15: for(i=0; i < N-1; i++) if(v[i] > v[i+1]){
16: //interschimbare
17: temp = v[i];
18: v[i] = v[i+1];
19: v[i+1] = temp;
20: sortat = false;
21: }
22: } while(!sortat);
23: }
24:
25: void bubbleSort(){
26: bool sortat;
27: int i, temp, n = N;
28: do{
29: sortat = true;
30: for(i=0; i < n-1; i++) if(v[i] > v[i+1]){
31: //interschimbare
32: temp = v[i];
33: v[i] = v[i+1];
34: v[i+1] = temp;
35: sortat = false;
36: }
37: n--;
38: } while(!sortat);
39: }
40:
41: int main(){
42: int i;
43: //vector initial
44: for(i=0; i< N; i++) cout << vInitial[i] << "\t"; cout << endl;
45:
46: //bubleSort neoptimizat, O(n^2), (n-1)^2 testari, maxim n(n-1)/2
interschimbari
47: for(i=0; i< N; i++) v[i] = vInitial[i];
48: bubbleSort1();
49: for(i=0; i< N; i++) cout << v[i] << "\t"; cout << endl;
50:
51: //bubleSort optimizat, O(n^2), n(n-1)/2 testari, maxim n(n-1)/2
interschimbari
52: for(i=0; i< N; i++) v[i] = vInitial[i];
53: bubbleSort();
54: for(i=0; i< N; i++) cout << v[i] << "\t"; cout << endl;
55:
56:
57: system("PAUSE");
58: return 0;
59: }
60:

You might also like