File: QuickSort.c

package info (click to toggle)
openmx 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 134,876 kB
  • sloc: ansic: 152,771; python: 876; makefile: 576; xml: 63; perl: 18; sh: 4
file content (123 lines) | stat: -rw-r--r-- 1,972 bytes parent folder | download
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
/**********************************************************************
  QuickSort.c:

     QuickSort.c is a subroutine to quick-sort an array a with
     an array b. 

  Log of QuickSort.c:

     08/Dec/2005  Released by T.Ozaki

***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "openmx_common.h"

typedef struct {
    double a,b;
} dlists;

typedef struct {
    int a,b;
} ilists;

typedef struct {
    int a,b,c;
} ilist3;


int dlists_cmp(const dlists *x, const dlists *y);
int ilists_cmp(const ilists *x, const ilists *y);


void qsort_int(long n, int *a, int *b)
{
  int i;
  ilists *AB;

  AB = (ilists*)malloc(sizeof(ilists)*n);

  for (i=0; i<n; i++){
    AB[i].a = a[i+1];
    AB[i].b = b[i+1];
  }

  qsort(AB, n, sizeof(ilists), (int(*)(const void*, const void*))ilists_cmp);

  for (i=0; i<n; i++){
    a[i+1] = AB[i].a;
    b[i+1] = AB[i].b;
  }

  free(AB);
}



void qsort_int3(long n, int *a, int *b, int *c)
{
  int i;
  ilist3 *ABC;

  ABC = (ilist3*)malloc(sizeof(ilist3)*n);

  for (i=0; i<n; i++){
    ABC[i].a = a[i];     
    ABC[i].b = b[i];
    ABC[i].c = c[i];
  }

  qsort(ABC, n, sizeof(ilist3), (int(*)(const void*, const void*))ilists_cmp);

  for (i=0; i<n; i++){
    a[i] = ABC[i].a;
    b[i] = ABC[i].b;
    c[i] = ABC[i].c;
  }

  free(ABC);
}




void qsort_double(long n, double *a, double *b)
{
  int i;
  dlists *AB;

  AB = (dlists*)malloc(sizeof(dlists)*n);

  for (i=0; i<n; i++){
    AB[i].a = a[i+1];     
    AB[i].b = b[i+1];
  }

  qsort(AB, n, sizeof(dlists), (int(*)(const void*, const void*))dlists_cmp);

  for (i=0; i<n; i++){
    a[i+1] = AB[i].a;
    b[i+1] = AB[i].b;
  }

  free(AB);
}


 
int dlists_cmp(const dlists *x, const dlists *y)
{
  return (x->a < y->a ? -1 :
          y->a < x->a ?  1 : 0);
}


int ilists_cmp(const ilists *x, const ilists *y)
{
  return (x->a < y->a ? -1 :
          y->a < x->a ?  1 : 0);
}