Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2013 20:11
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 anonymous/5430906 to your computer and use it in GitHub Desktop.
Save anonymous/5430906 to your computer and use it in GitHub Desktop.
// sequentialSort.c
//
// Copyright 2011 Armel Bourgon-Drouot armel.bourgon-drouot@esial.net
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 128000
int main(int argc, char **argv)
{
// arrayToSort is the array we will sort
int *arrayToSort = malloc(N * sizeof(int));
srand(time(NULL));
int i;
for(i=0;i<N;i++) {
arrayToSort[i] = rand()/100000;
}
double start = clock();
int sorted = 0;
while( sorted == 0) {
sorted= 1;
int i;
for(i=1;i<N-1; i += 2) {
if(arrayToSort[i] > arrayToSort[i+1])
{
int temp = arrayToSort[i+1];
arrayToSort[i+1] = arrayToSort[i];
arrayToSort[i] = temp;
sorted = 0;
}
}
for(i=0;i<N-1;i+=2) {
if(arrayToSort[i] > arrayToSort[i+1])
{
int temp = arrayToSort[i+1];
arrayToSort[i+1] = arrayToSort[i];
arrayToSort[i] = temp;
sorted = 0;
}
}
}
/*
printf("\nSorted array :\n");
for (i=0;i<N;i++) {
printf("%d; ",arrayToSort[i]);
}
*/
printf("sorted in %f s\n\n",((double)clock() - start) / CLOCKS_PER_SEC);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment