Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created July 23, 2012 04:49

Revisions

  1. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions configure
    Original file line number Diff line number Diff line change
    @@ -8,26 +8,26 @@ exec > Makefile
    echo "# Auto-Generated file - don't edit!"
    echo "# Created on $(date)"

    echo -e "\n"
    echo -e '\n'

    echo "cflags=-ggdb -Wall -Wextra -pedantic"
    echo "ldflags="
    echo 'cflags=-ggdb -Wall -Wextra -pedantic'
    echo 'ldflags='
    echo "ofiles=\$(patsubst %.c, %.o, $files)"
    echo "exe=sort"
    echo 'exe=sort'

    echo -e "\n"
    echo -e '\n'

    echo -e ".PHONY: clean all\n\t\n"
    echo -e "all: \$(exe)\n"
    echo "clean:"
    echo -e "\trm -f \$(exe) \$(ofiles)"
    echo -e "\t./configure\n"
    echo -e '.PHONY: clean all\n\t\n'
    echo -e 'all: $(exe)\n'
    echo 'clean:'
    echo -e '\trm -f $(exe) $(ofiles)'
    echo -e '\t./configure\n'

    echo "\$(exe): \$(ofiles)"
    echo -e "\tgcc \$(ldflags) $^ -o \$@\n"
    echo '$(exe): $(ofiles)'
    echo -e '\tgcc $(ldflags) $^ -o $@\n'

    for file in $files
    do
    gcc -MM $file
    echo -e "\tgcc \$(cflags) -c \$< -o \$@\n"
    echo -e '\tgcc $(cflags) -c $< -o $@\n'
    done
  2. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 1 changed file with 18 additions and 20 deletions.
    38 changes: 18 additions & 20 deletions configure
    Original file line number Diff line number Diff line change
    @@ -3,33 +3,31 @@
    # the echo jams everything onto one line
    files=$(echo `ls *.c`)

    rm -f Makefile
    touch Makefile
    exec > Makefile

    echo "# Auto-Generated file - don't edit!"
    echo "# Created on $(date)"

    echo "# Auto-Generated file - don't edit!" >> Makefile
    echo "# Created on $(date)" >> Makefile
    echo -e "\n"

    echo -e "\n" >> Makefile
    echo "cflags=-ggdb -Wall -Wextra -pedantic"
    echo "ldflags="
    echo "ofiles=\$(patsubst %.c, %.o, $files)"
    echo "exe=sort"

    echo "cflags=-ggdb -Wall -Wextra -pedantic" >> Makefile
    echo "ldflags=" >> Makefile
    echo "ofiles=\$(patsubst %.c, %.o, $files)" >> Makefile
    echo "exe=sort" >> Makefile
    echo -e "\n"

    echo -e "\n" >> Makefile
    echo -e ".PHONY: clean all\n\t\n"
    echo -e "all: \$(exe)\n"
    echo "clean:"
    echo -e "\trm -f \$(exe) \$(ofiles)"
    echo -e "\t./configure\n"

    echo -e ".PHONY: clean all\n\t\n" >> Makefile
    echo -e "all: \$(exe)\n" >> Makefile
    echo "clean:" >> Makefile
    echo -e "\trm -f \$(exe) \$(ofiles)" >> Makefile
    echo -e "\t./configure\n" >> Makefile

    echo "\$(exe): \$(ofiles)" >> Makefile
    echo -e "\tgcc \$(ldflags) $^ -o \$@\n" >> Makefile
    echo "\$(exe): \$(ofiles)"
    echo -e "\tgcc \$(ldflags) $^ -o \$@\n"

    for file in $files
    do
    gcc -MM $file >> Makefile
    echo -e "\tgcc \$(cflags) -c \$< -o \$@\n" >> Makefile
    gcc -MM $file
    echo -e "\tgcc \$(cflags) -c \$< -o \$@\n"
    done
  3. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 3 changed files with 20 additions and 12 deletions.
    2 changes: 1 addition & 1 deletion configure
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ echo "# Created on $(date)" >> Makefile

    echo -e "\n" >> Makefile

    echo "cflags=-ggdb -Wall -Wextra" >> Makefile
    echo "cflags=-ggdb -Wall -Wextra -pedantic" >> Makefile
    echo "ldflags=" >> Makefile
    echo "ofiles=\$(patsubst %.c, %.o, $files)" >> Makefile
    echo "exe=sort" >> Makefile
    14 changes: 9 additions & 5 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,17 @@
    int main()
    {

    const size_t length = 30;
    int * array1;
    int * array2;
    int * array3;
    size_t i;

    srand(time(0));

    size_t length = 30;
    int * array1 = (int*)malloc(length * sizeof(int));
    int * array2 = (int*)malloc(length * sizeof(int));
    int * array3 = (int*)malloc(length * sizeof(int));
    size_t i;
    array1 = (int*)malloc(length * sizeof(int));
    array2 = (int*)malloc(length * sizeof(int));
    array3 = (int*)malloc(length * sizeof(int));

    for(i = 0 ; i < length; ++i){
    printf("%d ", array1[i] = array2[i] = array3[i] = rand()%length);
    16 changes: 10 additions & 6 deletions mergesort.c
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,19 @@
    #include <stdlib.h>

    void mergesort(int * const A, const size_t L){
    if (L < 2) return;

    const size_t b = (L/2);// + sizeof(int);
    const size_t c = (L/2) + (L%2);// + sizeof(int);
    size_t b, c, i, j, k;

    int * B;
    int * C;

    if (L < 2) return;

    size_t i, j, k;
    b = (L/2);
    c = (L/2) + (L%2);

    int * const B = (int*) malloc(b * sizeof(int));
    int * const C = (int*) malloc(c * sizeof(int));
    B = (int*) malloc(b * sizeof(int));
    C = (int*) malloc(c * sizeof(int));

    memcpy(B, A, b * sizeof(int));
    memcpy(C, A + b, c * sizeof(int));
  4. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 3 changed files with 50 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion main.c
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@

    #include "heapsort.h"
    #include "quicksort.h"
    #include "mergesort.h"

    int main()
    {
    @@ -13,17 +14,20 @@ int main()
    size_t length = 30;
    int * array1 = (int*)malloc(length * sizeof(int));
    int * array2 = (int*)malloc(length * sizeof(int));
    int * array3 = (int*)malloc(length * sizeof(int));
    size_t i;

    for(i = 0 ; i < length; ++i){
    printf("%d ", array1[i] = array2[i] = rand()%length);
    printf("%d ", array1[i] = array2[i] = array3[i] = rand()%length);
    }
    printf("\n");

    puts("Heapsorting...");
    heapsort(array1, length);
    puts("Quicksorting...");
    quicksort(array2, length);
    puts("Mergesorting...");
    mergesort(array3, length);

    puts("Heapsort output:");
    for(i = 0 ; i < length; ++i){
    @@ -37,5 +41,11 @@ int main()
    }
    printf("\n");

    puts("Mergesort output:");
    for(i = 0 ; i < length; ++i){
    printf("%d ", array3[i]);
    }
    printf("\n");

    return 0;
    }
    31 changes: 31 additions & 0 deletions mergesort.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #include <string.h>
    #include <stdlib.h>

    void mergesort(int * const A, const size_t L){
    if (L < 2) return;

    const size_t b = (L/2);// + sizeof(int);
    const size_t c = (L/2) + (L%2);// + sizeof(int);

    size_t i, j, k;

    int * const B = (int*) malloc(b * sizeof(int));
    int * const C = (int*) malloc(c * sizeof(int));

    memcpy(B, A, b * sizeof(int));
    memcpy(C, A + b, c * sizeof(int));

    mergesort(B, b);
    mergesort(C, c);

    for(i=j=k=0; j<b && k<c; A[i++] = C[k]<B[j] ? C[k++] : B[j++]);

    if(j < b)
    memcpy(A + i, B + j, (L - i) * sizeof(int));
    else
    memcpy(A + i, C + k, (L - i) * sizeof(int));

    free(B);
    free(C);
    }

    8 changes: 8 additions & 0 deletions mergesort.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #ifndef MERGESORT_H
    #define MERGESORT_H

    #include <stdlib.h>

    void mergesort(int * const, const size_t);

    #endif
  5. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion dummy.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    deleteme!
  6. Gordon Bailey (iMac-Ubuntu) revised this gist Jul 23, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions dummy.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    deleteme!
  7. grodtron created this gist Jul 23, 2012.
    1 change: 1 addition & 0 deletions dummy.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    deleteme!
  8. Gordon Bailey (iMac-Ubuntu) created this gist Jul 23, 2012.
    4 changes: 4 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    *.swp
    *.o
    sort
    Makefile
    35 changes: 35 additions & 0 deletions configure
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/bash

    # the echo jams everything onto one line
    files=$(echo `ls *.c`)

    rm -f Makefile
    touch Makefile


    echo "# Auto-Generated file - don't edit!" >> Makefile
    echo "# Created on $(date)" >> Makefile

    echo -e "\n" >> Makefile

    echo "cflags=-ggdb -Wall -Wextra" >> Makefile
    echo "ldflags=" >> Makefile
    echo "ofiles=\$(patsubst %.c, %.o, $files)" >> Makefile
    echo "exe=sort" >> Makefile

    echo -e "\n" >> Makefile

    echo -e ".PHONY: clean all\n\t\n" >> Makefile
    echo -e "all: \$(exe)\n" >> Makefile
    echo "clean:" >> Makefile
    echo -e "\trm -f \$(exe) \$(ofiles)" >> Makefile
    echo -e "\t./configure\n" >> Makefile

    echo "\$(exe): \$(ofiles)" >> Makefile
    echo -e "\tgcc \$(ldflags) $^ -o \$@\n" >> Makefile

    for file in $files
    do
    gcc -MM $file >> Makefile
    echo -e "\tgcc \$(cflags) -c \$< -o \$@\n" >> Makefile
    done
    19 changes: 19 additions & 0 deletions heapsort.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #include <stdlib.h>
    #include "swap.h"

    void heapsort(int * const A, const size_t L){
    size_t i, j;
    int * B;
    for (i = 0; i < L; ++i)
    for (j = i/2; i; i /= 2, j = i/2)
    if (A[i] > A[j]) swap(A + i, A + j);
    else break;
    for (B = A + L - 1; A != B; --B){
    swap(A, B);
    for (i = 0, j = 1; A + j < B; j = (2*i) + 1){
    if (A + j + 1 < B) j = A[j] > A[j+1] ? j : j+1;
    if (A[i] < A[j]) swap(A + i, A + j), i = j;
    else break;
    }
    }
    }
    8 changes: 8 additions & 0 deletions heapsort.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #ifndef HEAPSORT_H
    #define HEAPSORT_H

    #include <stdlib.h>

    void heapsort(int*const, const size_t);

    #endif
    41 changes: 41 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>

    #include "heapsort.h"
    #include "quicksort.h"

    int main()
    {

    srand(time(0));

    size_t length = 30;
    int * array1 = (int*)malloc(length * sizeof(int));
    int * array2 = (int*)malloc(length * sizeof(int));
    size_t i;

    for(i = 0 ; i < length; ++i){
    printf("%d ", array1[i] = array2[i] = rand()%length);
    }
    printf("\n");

    puts("Heapsorting...");
    heapsort(array1, length);
    puts("Quicksorting...");
    quicksort(array2, length);

    puts("Heapsort output:");
    for(i = 0 ; i < length; ++i){
    printf("%d ", array1[i]);
    }
    printf("\n");

    puts("Quicksort output:");
    for(i = 0 ; i < length; ++i){
    printf("%d ", array2[i]);
    }
    printf("\n");

    return 0;
    }
    36 changes: 36 additions & 0 deletions quicksort.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #include <stdlib.h>
    #include "swap.h"

    void quicksort(int * const A, const size_t Ln){
    int * R = A + Ln - 1;
    int * L = A;
    int Ld = 1;
    int Rd = 1;
    int P = *(A + (Ln/2));
    if (Ln < 2) return;
    while(1){
    while(*R >= P){
    if(*R > P) --R;
    else{
    while(*(R - Rd) == P && (R - Rd) > L) ++Rd;
    if((R - Rd) > L) swap(R, R - Rd);
    else break;
    }
    }
    while(*L <= P){
    if(*L < P) ++L;
    else{
    while(*(L + Ld) == P && (L + Ld) < R) ++Ld;
    if((L + Ld) < R) swap(L, L + Ld);
    else break;
    }
    }
    if(*L > P || *R < P){
    swap(L, R);
    }else{
    break;
    }
    }
    quicksort(A, (size_t) (L - A));
    quicksort(R + 1, (size_t) Ln - (R + 1 - A));
    }
    8 changes: 8 additions & 0 deletions quicksort.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #ifndef QUICKSORT_H
    #define QUICKSORT_H

    #include <stdlib.h>

    void quicksort(int * const, const size_t);

    #endif
    5 changes: 5 additions & 0 deletions swap.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    void swap(int* a, int* b){
    int c = *a;
    *a = *b;
    *b = c;
    }
    6 changes: 6 additions & 0 deletions swap.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #ifndef SWAP_H
    #define SWAP_H

    void swap(int*, int*);

    #endif