Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

FUCK YEAH BUBBLE SORT

Name: Anonymous 2008-07-09 1:39


inline void swap_bytes(void *a, void *b, size_t size)
{
    char *a_char = a, *b_char = b, temp;

    while(size--) {
        temp      = *a_char;
        *a_char++ = *b_char;
        *b_char++ = temp;
    }
}

void bubble_sort(void *base, size_t num, size_t size, int (*comparator)(const void *, const void *))
{
    void *first, *second;
    size_t pass, passes = num - 1, i;
    int result;

    if(num > 1) {
        for(pass = 0; pass < passes; ++pass) {
            second = base + size * num - size;
            first  = second - size;

            for(i = num; i > pass + 1; --i) {
                result = comparator(first, second);
                if(result == 1)
                    swap_bytes(first, second, size);
                first -= size;
                second -= size;
            }
        }
    }
}


Here you go, /prog/, a generic implementation of bubble sort that can replace qsort().

Name: Anonymous 2008-07-11 14:43

RANDOMSORT IS SUPERIOR

#include <cstdio>
using namespace std;

bool isSorted(int* list, int l)
{
  for (int i=1; i<l; i++)
    if (list[i-1]>list[i])
      return false;
  return true;
}

int*& randomSort(int*& list, int l)
{
  int i;
  int j;
  int temp;
  srand(time(0));

  while (!isSorted(list,l))
    {
      i=rand()%l;
      j=rand()%l;
      temp = list[i];
      list[i] = list[j];
      list[j] = temp;
    }
  return list;
}
int main(void)
{
  int k=14;
  int* a = new int[15];
  for (int i=0; i<15; i++)
    a[i]=k--;
  a = randomSort(a,15);
  for(int i=0; i<15; i++)
    printf("%d ",a[i]);
  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List