Last active
February 21, 2019 18:36
-
-
Save aaronang/488b915e747229ef157316992065b19d to your computer and use it in GitHub Desktop.
Interview Question: Review the handwritten sorting library in C++.
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 <cstdlib> | |
#include <iostream> | |
#include <sorter.hpp> | |
int main (int argc, char *argv[]) { | |
int* a = new int[10]; | |
for (int i = 0; i < 10; i++) { | |
a[i] = rand(); | |
} | |
Sorter* s = new Sorter(); | |
s->sort(a, 10); | |
for (int i = 0; i < 10; i++) { | |
cout << a[i] << endl; | |
} | |
} |
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 <sorter.hpp> | |
void Sorter::sort(int* a, int s) { | |
for (int i = 0; i < s; i++) | |
for (int j = 1; j < s; j++) | |
if (a[j] < a[j - 1]) { | |
int temp = a[j]; | |
a[j] = a[j - 1]; | |
a[j - 1 ] = temp; | |
} | |
} |
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
using namespace std; | |
struct Sorter { | |
void sort(int* a, int s); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment