Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created May 18, 2015 22:15
Show Gist options
  • Save krysseltillada/5a54fd897d4f77bb3a71 to your computer and use it in GitHub Desktop.
Save krysseltillada/5a54fd897d4f77bb3a71 to your computer and use it in GitHub Desktop.
sorting ascending to descending(bubble sort)
#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