原理

 

qsort

 

void qsort (void* base, size_t num, size_t size,
            int (*compar)(const void*,const void*));

 

Sort elements of array

Sorts the num elements of the array pointed by base, each element size bytes long, using the compar function to determine the order.

The sorting algorithm used by this function compares pairs of elements by calling the specified compar function with pointers to them as argument.

The function does not return any value, but modifies the content of the array pointed by base reordering its elements as defined by compar.

The order of equivalent elements is undefined.

Parameters

 

base
Pointer to the first object of the array to be sorted, converted to a void*.
num
Number of elements in the array pointed by base.
size_t is an unsigned integral type.
size
Size in bytes of each element in the array.
size_t is an unsigned integral type.
compar
Pointer to a function that compares two elements.
This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:
 
int compar (const void* p1, const void* p2);

Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):
return valuemeaning
<0 The element pointed by p1 goes before the element pointed by p2
0 The element pointed by p1 is equivalent to the element pointed by p2
>0 The element pointed by p1 goes after the element pointed by p2

For types that can be compared using regular relational operators, a general compar function may look like:

1
2
3
4
5
6
int compareMyType (const void * a, const void * b)
{
  if ( *(MyType*)a <  *(MyType*)b ) return -1;
  if ( *(MyType*)a == *(MyType*)b ) return 0;
  if ( *(MyType*)a >  *(MyType*)b ) return 1;
}

 

實例:

#include <stdio.h>

#include <stdlib.h>

#include<time.h>

 

int compare(const void *a, const void *b)//這函式是 qsort 所需的比較函式
{
int c = *(int *)a;
int d = *(int *)b;
if(c < d) {return -1;} //傳回 -1 代表 a < b
else if (c == d) {return 0;} //傳回 0 代表 a = b
else return 1; //傳回 1 代表 a>b
}

/* 字串部分比較 可以利用下列部分

int compare( const void *a, const void *b)

{

return( strcmp((char *)a,(char *)b) );

}

*/

 

void main()

{

int a[100],i;

srand((unsigned) time(NULL));

for(i=0;i<100;i++)
a[i]=rand(); //亂數產生100個數字

for(unsigned i = 0; i < 100; i++)
{
printf(" %d\t ", a[i]); //印出排序前的內容
}

printf("\n");

/*qsort 函數說明 ->>需要 #include<stdlib.h>

void qsort(void* base, size_t n, size_t size, int (*cmp)(const void*, const void*))

陣列的快速排序法, base是起始陣列位址,n 是陣列大小,size 是每個元素的大小,最後的參數是指向函數的指標,這是比較元素大小的函數( 即上面 compare() 函數)。

*/

qsort((void *)a, 100, sizeof(a[0]), compare);

for(unsigned i = 0; i < 100; i++)

{

printf(" %d\t", a[i]); //印出排序後的內容

}

system("PAUSE");

}

 

=============簡單一點的==============================

 

/* qsort example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 winage 的頭像
    winage

    winage的部落格

    winage 發表在 痞客邦 留言(0) 人氣()