KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
LOOT THE DOG – anyone who's ever raided MC

N^2 sort comparison

Gnome sort was marginally faster than Bubble sort (so, no reason ever to use Bubble sort), but Insertion sort was twice as fast as gnome sort, IIRC.

Oh yeah, my srand value was stupid. Don't mind that.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define NUM_ITEMS 15000
#define RUNS 3

void gnomeSort(int arr[], int n);
void bubbleSort(int arr[], int n);
void insertionSort(int arr[], int n);

typedef void (*Sort)(int arr[], int n);

int numbers[NUM_ITEMS];
int runtimes[RUNS];

int main(){
    int i, j, k;
    long start, end, avg;

    Sort funcs[] = {bubbleSort, gnomeSort, insertionSort};
    char* func_names[] = {"bubble", "gnome", "insertion"};

    for(k=0; k<3; k++){
        printf("Starting %s sort\n", func_names[k]);
        for(j=0; j<RUNS; j++){
            //printf("Seeding random number generator\n");
            srand(clock()+getpid()); //seed random number generator

            //printf("Filling random numbers\n");
            for (i=0; i<NUM_ITEMS; i++)
                numbers[i] = rand(); //fill array with random integers

            start = clock();
            //printf("Clock ticks before %s sort: %d\n", func_names[k], start);
            funcs[k](numbers, NUM_ITEMS); //perform gnome sort on array
            end = clock();
            //printf("Clock ticks after %s sort: %d\n", func_names[k], end);
            //printf("Total ticks: %d\n", end-start);
            runtimes[j] = end-start;
        }
        printf("\nDone with %s sort. Runtimes are:\n", func_names[k]);
        for (i=0; i<RUNS; i++)
            printf("%i\n", runtimes[i]);
        printf("\nAverage time is: ");
        avg = 0;
        for(i=0; i<RUNS; i++){
            avg+=runtimes[i];
        }
        printf("%d\n\n", avg/RUNS);
    }
}


void gnomeSort(int arr[], int n) {
    int i=0, temp;

    while(i < n) {
        if(i == 0 || arr[i-1] <= arr[i]) i++;
        else{
            temp = arr[i];
            arr[i] = arr[i-1];
            arr[--i] = temp;
        }
    }
}

void bubbleSort(int arr[], int n){
    int i, j, temp;

    for(i=n-1; i>0; i--){
        for(j=1; j<=i; j++){
            if(arr[j-1] > arr[j]){
                temp = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

void insertionSort(int arr[], int n){
    int i, j, current;

    for(i=1; i<n; i++){
        current = arr[i];
        for(j=i; j>0 && arr[j-1] > current; j--)
            arr[j] = arr[j-1];
        arr[j] = current;
    }
}

Page last edited: November 29, 2005 (utc)

Index

A B C D E F G H I J L M N O P R S T U V W X

All pages

A

  1. About
  2. Acno's Energizer
  3. Artificial Intelligence
  4. ASP.NET
  5. Atom

B

  1. Bash
  2. Belief systems
  3. Bookmarklets
  4. Build tools

C

  1. C and C++
  2. C#
  3. C++ Reference
  4. Calvinism
  5. Cars I want to consider
  6. CGI
  7. character sets
  8. Chess
  9. Christian Reconstruction
  10. Christian Resources
  11. Chronicles of Narnia
  12. Color tools
  13. Computer Science
  14. Cornelius Van Til
  15. CSS - Cascading style sheets
  16. CSSTabs

D

  1. Database
  2. Differencing programs
  3. Documentation standards
  4. Downloads
  5. Dualities
  6. Dvorak keyboard

E

  1. E-mail me
  2. Eclipse
  3. Eiffel
  4. Emacs
  5. Evolution
  6. Extension languages

F

  1. File extensions
  2. Firefox
  3. Formation: web form automation library for PHP
  4. Forth

G

  1. Greg Bahnsen
  2. GUI Toolkits
  3. Guns

H

  1. Hex editors

I

  1. Important articles or essays
  2. Installers
  3. Internet radio stations

J

  1. Java
  2. Javascript
  3. jEdit

L

  1. Linux
  2. Lisp
  3. Logical fallacies
  4. Lua

M

  1. Markup
  2. Miscellaneous Links
  3. mod_rewrite
  4. Movie theaters
  5. My comment policy
  6. My essential programs
  7. My resume

N

  1. Namespaces
  2. Naming conventions
  3. New Years 2000
  4. N^2 sort comparison

O

  1. Open Source License
  2. OPML

P

  1. Perl
  2. Philosophy
  3. PHP
  4. PHP Calendar (version 2.3)
  5. PHP XML Library, version 1.2b
  6. Pictures
  7. Postmillenialism
  8. Presuppositionalism
  9. Programming Fonts
  10. Programming languages
  11. Programming Resources
  12. Punta Cana
  13. Python

R

  1. RDF
  2. REBOL
  3. Reflex game
  4. Regular expressions
  5. Religion
  6. RFCs
  7. Robot Exclusions
  8. Roman Catholicism
  9. Ruby

S

  1. Scala programming language
  2. Science
  3. Shorthand
  4. Skydiving, August 28, 2000
  5. Software I've written
  6. SPAM
  7. SQLite
  8. StructuredText

T

  1. Tabs vs Spaces
  2. Tcl/Tk
  3. Tea
  4. Text Editors
  5. TextDrive
  6. The Big Bang
  7. The naked street
  8. Theonomy
  9. Tools of communication

U

  1. Unicode
  2. URL Design

V

  1. Version control systems
  2. VI text editor
  3. Virtual machines

W

  1. WeblogUrls
  2. Wiki
  3. WikiBlogIntegration
  4. World of Warcraft
  5. wxWidgets

X

  1. XHTML
  2. XML
  3. XML to PHP translator
  4. XML-RPC
  5. XML-RPC Library for PHP (v 2.5)

Generated in about 0.034s.

(Used 4 db queries)

mobile phone