Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int as = 0;
- int comp = 0;
- void selection_sort(int a[], int n){
- int min;
- int i, j;
- for(i = 0; i < n-1; i++){
- int minInd = i;
- for(j = i+1; j < n; j++){
- comp++;
- if(a[j]<a[minInd]) minInd = j;
- }
- if(minInd!=i){
- as++;
- a[i] = a[i]^a[minInd];
- a[minInd] = a[i]^a[minInd];
- a[i] = a[i]^a[minInd];
- }
- }
- }
- void bubble_sort(int a[], int n){
- int i, j;
- bool swapped;
- for(i = n-2; i >= 0; i--){
- swapped = false;
- for(j = 0; j <=i; j++){
- comp++;
- if(a[j] > a[j+1]){
- as++;
- a[j] = a[j] ^ a[j+1];
- a[j+1] = a[j] ^ a[j+1];
- a[j] = a[j] ^ a[j+1];
- swapped = true;
- }
- }
- if(swapped==false) break;
- }
- }
- int main()
- {
- int arr[] = {1,3,5,6,4,2};
- int sz = 7;
- selection_sort(arr,sz);
- cout << "selection_sort" << endl;
- for(int i = 0; i < sz; i++){
- cout << arr[i];
- }
- cout << "\nas = " << as << " comp = " << comp << endl;
- as = 0;
- comp = 0;
- int arr1[] = {1,3,5,6,4,2};
- cout << "bubble_sort" << endl;
- bubble_sort(arr1,sz);
- for(int i = 0; i < sz; i++){
- cout << arr[i];
- }
- cout << "\nas = " << as << " comp = " << comp << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement