Skip to content

Instantly share code, notes, and snippets.

@bhargavkulk
Created September 30, 2018 09:17
Show Gist options
  • Save bhargavkulk/8e150211005cee27d0182104889448c4 to your computer and use it in GitHub Desktop.
Save bhargavkulk/8e150211005cee27d0182104889448c4 to your computer and use it in GitHub Desktop.
struct Int {
int i;
Int(int a=0) { i = a; }
void write(ofstream&);
void read(ifstream&);
};
void sort() {
ifstream fin("ints.dat");
ofstream fout("temp.dat");
Int temp, *ptr; // this can be an array
//shove it int an array
int i = 0;
while(fin.read((char*)temp, sizeof(Int))) {
ptr[i++] = temp;
}
//sort the array
for(int j=0; j<i-1; j++) {
for(int k=0; k<i-1-j; k++) {
if(ptr[k].i > ptr[k+1].i) {
Int temp = ptr[k];
ptr[k] = ptr[k+1];
ptr[k+1] = ptr[k];
}
}
}
//shove it into the new file
for(j=0; j<i; j++) {
fout.write((char*)temp, sizeof(Int))
}
remove("ints.dat");
rename("temp.dat", "ints.dat");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment