source: trunk/Softwares/Basic_test.or32/src/c/func_array.c @ 2

Last change on this file since 2 was 2, checked in by kane, 17 years ago

Import Morpheo

File size: 3.3 KB
Line 
1#include "func_array.h"
2
3//-----[ Selection_sort ]--------------------------------------------------
4
5void selection_sort  (int array[], int size) 
6{
7  int i, min, j , tmp;
8  for(i = 0 ; i < size - 1 ; i++) 
9    {
10      min = i;
11
12      // Find indice min
13      for(j = i+1 ; j < size ; j++)
14        if(array[j] < array[min])
15          min = j;
16
17      if(min != i) // find or not.
18        {
19          tmp        = array[i];
20          array[i]   = array[min];
21          array[min] = tmp;
22        }
23    }
24}
25
26//-----[ insertion_sort ]--------------------------------------------------
27void insertion_sort (int array[], int size) 
28{
29  /* Spécifications externes : Tri du tableau t par insertion séquentielle */
30  int i,pos,j,tmp;
31
32  for (i = 1 ; i < size ; i++) 
33    {
34      /* Calcul de la position d'insertion pos : */
35      /* détermine le plus petit indice pos       / 0 <= pos <= i */
36      /* qui vérifie array[pos] >= array[i] */
37      pos = 0;
38     
39      while(array[pos] < array[i]) 
40        pos++;
41     
42      tmp = array[i]; /* Sauvegarde de t[i] */
43 
44      for(j = i-1 ; j >= pos  ; j--)
45        array[j+1] = array[j]; /* translation de t[pos..i-1] vers t[pos+1..i] */
46     
47      array[pos] = tmp; /* insertion de t[pos] */
48    }
49}
50
51//-----[ QuickSort ]-------------------------------------------------------
52
53// QuickSort
54void quick_sort (int * array, int debut, int fin)
55{
56  int          gauche, droite;
57  int          pivot;
58
59  pivot = array[debut];
60
61  for(gauche = debut, droite = fin; gauche < droite; )
62    {
63     
64      for(;array[droite] > pivot; droite--);
65           
66      if(gauche != droite)
67        {
68          array[gauche] = array[droite];
69          array[droite] = pivot;
70          gauche++;
71        }
72     
73      for(; array[gauche] < pivot; gauche++);
74           
75      if(gauche != droite)
76        { 
77          array[droite] = array[gauche];
78          array[gauche] = pivot;
79          droite--;
80        }
81    }
82 
83  if(debut < gauche-1) quick_sort(array, debut   , gauche-1);
84  if(fin   > gauche+1) quick_sort(array, gauche+1, fin     );
85}
86
87//-----[ BubbleSort ]------------------------------------------------------
88
89void bubble_sort (int array[], unsigned int size) 
90{
91  int i   = 0; /* Indice de répétition du tri */
92  int j   = 0; /* Variable de boucle */
93  int tmp = 0; /* Variable de stockage temporaire */
94 
95  /* Booléen marquant l'arrêt du tri si le tableau est ordonné */
96  int en_desordre = 1; 
97 
98  /* Boucle de répétition du tri et le test qui
99     arrête le tri dès que le tableau est ordonné */
100  for(i = 0 ; (i < size) && (en_desordre == 1); i++)
101    { 
102      /* Supposons le tableau ordonné */
103      en_desordre = 0;
104     
105                /* Vérification des éléments des places j et j-1 */
106      for(j = 1 ; j < size - i ; j++) 
107        {
108          /* Si les 2 éléments sont mal triés */
109          if(array[j] < array[j-1])
110            {
111              /* Inversion des 2 éléments */
112              tmp        = array[j-1];
113              array[j-1] = array[j];
114              array[j]   = tmp;
115             
116              /* Le tableau n'est toujours pas trié */
117              en_desordre = 1;
118            }
119        }
120    }
121}
122
123//-----[ Algo ]------------------------------------------------------------
124
125void array_tri_croissant (int * array, unsigned int size,int algo)
126{
127  switch (algo)
128    {
129    case 0 : bubble_sort    (array,size);     break;
130    case 1 : selection_sort (array,size);     break;
131    case 2 : insertion_sort (array,size);     break;
132    case 3 : quick_sort     (array,0,size-1); break;
133    default : break;
134    }
135}
136
Note: See TracBrowser for help on using the repository browser.