Last active
August 29, 2015 14:06
-
-
Save neizod/69dc54166a3dc614de03 to your computer and use it in GitHub Desktop.
Sorting Algorithms
This file contains 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> | |
using namespace std; | |
int arr[] = {2,5,3,9,1,0,7,4,8,6}; | |
void show(int i=-1) { | |
for (int k=0; k<10; k++) { | |
if (i-1 == k) cout << "\x1b[37;1m"; | |
if (i == k) cout << "\x1b[31;1m"; | |
cout << arr[k]; | |
if (i == k) cout << "\x1b[0m"; | |
} | |
cout << endl; | |
} | |
int main(void) { | |
show(); | |
bool swapped = false; | |
do { | |
swapped = false; | |
for (int i=1; i<10; i++) { | |
if (arr[i-1] > arr[i]) { | |
arr[i] ^= arr[i-1] ^= arr[i] ^= arr[i-1]; | |
swapped = true; | |
show(i); | |
} | |
} | |
} while (swapped); | |
show(); | |
return 0; | |
} |
This file contains 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> | |
using namespace std; | |
int arr[] = {2,5,3,9,1,0,7,4,8,6}; | |
void show(int i=-1, int j=10) { | |
for (int k=0; k<10; k++) { | |
if (i >= k) cout << "\x1b[37;44m"; | |
if (j == k) cout << "\x1b[31;1m"; | |
if (j+1 == k) cout << "\x1b[37;1m"; | |
cout << arr[k]; | |
if (i >= k) cout << "\x1b[0m"; | |
} | |
cout << endl; | |
} | |
int main(void) { | |
show(); | |
for (int i=0; i<10; i++) { | |
int j = i; | |
while (j > 0 && arr[j-1] > arr[j]) { | |
arr[j] ^= arr[j-1] ^= arr[j] ^= arr[j-1]; | |
j -= 1; | |
show(i, j); | |
} | |
} | |
show(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment