1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright (C) 2001, 2002, and 2003 Roy Keene
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* email: dact@rkeene.org
*/
#include "dact.h"
#include "sort.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
/*
Bubble Sort
*/
void int_sort (uint32_t array[], unsigned int listsize, int mode) {
uint32_t *mod_array=NULL;
unsigned int temp,i,j;
mode=(!!mode);
if (mode) {
mod_array=malloc(listsize*sizeof(uint32_t));
for (i=0;i<listsize;i++) mod_array[i]=i;
}
for (i=0;i<(listsize);i++) {
for (j=0;j<(listsize-1);j++) {
if (array[j] < array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
switch (mode) {
case 0 : break;
case 1 :
temp=mod_array[j];
mod_array[j]=mod_array[j+1];
mod_array[j+1]=temp;
}
}
}
}
if (mode) {
memcpy(array, mod_array, listsize*sizeof(uint32_t));
free(mod_array);
}
}
/*
I have no idea what this sort is called, if it has a name
(I just made one that I thought would work well.)
*/
void int_sort_fast (uint32_t array[], unsigned int elements, int mode) {
uint32_t *hold_array;
uint32_t *mod_array=NULL;
unsigned int i, x, cnt=0;
unsigned char sizeint=sizeof(uint32_t);
mode=(!!mode);
hold_array=calloc((elements+1),sizeint);
if (mode) {
mod_array=malloc(elements*sizeint);
for (i=0;i<elements;i++) mod_array[i]=i;
}
for (i=0;i<elements;i++) {
if (array[i]==0) continue;
for (x=0;x<cnt+1;x++) {
if (array[i] > hold_array[x]) {
if (x<cnt) {
memmove(hold_array+x+1,hold_array+x,(cnt-x+1)*sizeint);
}
hold_array[x]=array[i];
switch (mode) {
case 0: break;
case 1:
memmove(mod_array+x+1,mod_array+x,(cnt-x+1)*sizeint);
mod_array[x]=i;
}
break;
}
}
cnt++;
}
if (mode) {
memcpy(array,mod_array,elements*sizeint);
free(mod_array);
} else {
memcpy(array,hold_array,elements*sizeint);
}
free(hold_array);
}
/*
Sort really quickly
*/
void int_sort_really_fast (uint32_t array[], unsigned int elements, int mode) {
uint16_t list_items[0x10000];
/* uint16_t list_items[0x10000][0x10001]; */
int i,f=0,m;
memset(list_items,0,sizeof(list_items));
for (i=0;i<elements;i++) {
list_items[array[i]]++;
}
for (i=0xffff;i;i--) {
if (list_items[i]!=0) {
for (m=0;m<list_items[i];m++) array[f++]=i;
}
}
return;
}
|