Created
May 18, 2015 22:15
-
-
Save krysseltillada/5a54fd897d4f77bb3a71 to your computer and use it in GitHub Desktop.
sorting ascending to descending(bubble sort)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() | |
{ | |
const int MAXVAL = 5; | |
int num[MAXVAL] = {22 , 1, 33, 9, 10}; | |
int temp = 0; | |
for(int i = 0; i < MAXVAL; i++) { /// sorts in descending order | |
for(int n = i + 1; n < MAXVAL; n++) { | |
if(num[i] > num[n]){ | |
temp = num[n]; | |
num[n] = num[i]; | |
num[i] = temp; | |
} else { | |
num[n] = num[n]; | |
} | |
} | |
} | |
for(auto i : num) /// auto deduces the array num as int /// for every i in num | |
std::cout << i << " "; | |
std::cout << std::endl; | |
for(int i = 0; i < MAXVAL; i++) { /// sorts in ascending order | |
for(int n = i + 1; n < MAXVAL; n++) { | |
if(num[i] < num[n]){ | |
temp = num[n]; | |
num[n] = num[i]; | |
num[i] = temp; | |
} else { | |
num[n] = num[n]; | |
} | |
} | |
} | |
for(auto a : num) /// deduces the array num in int /// for every a in num | |
std::cout << a << " "; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment