Thu Nov 13, 2008 3:06 pm
#include <iostream>
#include <algorithm>
using namespace std;
/*
* This program reads in integers and sorts them using the library sort
* function. The algorithm header provides the sorting function.
*
* Author: Tom Bennet
*/
const int MAX_NUM_INTS = 100;
int main()
{
int ints[MAX_NUM_INTS]; // Where the numbers go.
// Read them in.
int i;
for(i = 0; i < MAX_NUM_INTS && cin >> ints[i]; ++i);
int numints = i;
// Sort them. The library sort algorithm wants a pointer to the
// the start of the data, and a pointer one past the end.
sort(ints, ints + numints);
// Print them.
cout << "==================" << endl;
for(int i = 0; i < numints; ++i)
cout << ints[i] << endl;
cout << "==================" << endl;
}
Wed Jan 18, 2012 3:23 pm
Sun Jan 27, 2013 9:15 pm
#include <stdlib.h>
#include <string.h>
#include "sort.h"
int issort(void *data, int size, int esize, int (*compare)(const void *key1,
const void *key2)) {
char *a = data;
void *key;
int i,
j;
if ((key = (char *)malloc(esize)) == NULL)
return -1;
for (j = 1; j < size; j++) {
memcpy(key, &a[j * esize], esize);
i = j - 1;
while (i >= 0 && compare(&a[i * esize], key) > 0) {
memcpy(&a[(i + 1) * esize], &a[i * esize], esize);
i--;
}
memcpy(&a[(i + 1) * esize], key, esize);
}
free(key);
return 0;
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.