Skip to content

Instantly share code, notes, and snippets.

@aaronang
Last active February 21, 2019 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronang/488b915e747229ef157316992065b19d to your computer and use it in GitHub Desktop.
Save aaronang/488b915e747229ef157316992065b19d to your computer and use it in GitHub Desktop.
Interview Question: Review the handwritten sorting library in C++.
#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;
}
}
#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;
}
}
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