當前位置:首頁 » 操作系統 » qsort源碼

qsort源碼

發布時間: 2023-06-01 00:13:08

『壹』 C和C++如何混合編程

在用C++的項目源碼中,經常會不可避免的會看到下面的代碼:

1
#ifdef __cplusplus

2
extern "C" {

3
#endif

4

5
/*...*/

6

7
#ifdef __cplusplus

8
}

9
#endif

它到底有什麼用呢,你知道嗎?而且這樣的問題經常會出現在面試or筆試中。下面我就從以下幾個方面來介紹它:

1、#ifdef _cplusplus/#endif _cplusplus及發散
2、extern "C"
2.1、extern關鍵字
2.2、"C"
2.3、小結extern "C"
3、C和C++互相調用 4、C和C++混合調用特別之處函數指針
3.1、C++的編譯和連接
3.2、C的編譯和連接
3.3、C++中調用C的代碼
3.4、C中調用C++的代碼

1、#ifdef _cplusplus/#endif _cplusplus及發散

在介紹extern "C"之前,我們來看下#ifdef
_cplusplus/#endif
_cplusplus的作用。很明顯#ifdef/#endif、#ifndef/#endif用於條件編譯,#ifdef
_cplusplus/#endif
_cplusplus——表示如果定義了宏_cplusplus,就執行#ifdef/#endif之間的語句,否則就不執行。

在這里為什麼需要#ifdef _cplusplus/#endif
_cplusplus呢?因為c語言中不支持extern "C"聲明,如果你明白extern
"C"的作用就知道在C中也沒有必要這樣做,這就是條件編譯的作用!在.c文件中包含了extern "C"時會出現編譯時錯誤。

既然說到了條件編譯,我就介紹它的一個重要應用——避免重復包含頭文件。還記得騰訊筆試就考過這個題目,給出類似下面的代碼(下面是我最近在研究的一個開源web伺服器——Mongoose的頭文件mongoose.h中的一段代碼):

01
#ifndef MONGOOSE_HEADER_INCLUDED

02
#define MONGOOSE_HEADER_INCLUDED

03

04
#ifdef __cplusplus

05
extern "C" {

06
#endif /* __cplusplus */

07

08
/*.................................

09
* do something here

10
*.................................

11
*/

12

13
#ifdef __cplusplus

14
}

15
#endif /* __cplusplus */

16

17
#endif /* MONGOOSE_HEADER_INCLUDED */

然後叫你說明上面宏#ifndef/#endif的作用?為了解釋一個問題,我們先來看兩個事實:

這個頭文件mongoose.h可能在項目中被多個源文件包含(#include

"mongoose.h"),而對於一個大型項目來說,這些冗餘可能導致錯誤,因為一個頭文件包含類定義或inline函數,在一個源文件中mongoose.h可能會被#include兩次(如,a.h頭文件包含了mongoose.h,而在b.c文件中#include
a.h和mongoose.h)——這就會出錯(在同一個源文件中一個結構體、類等被定義了兩次)。
從邏輯觀點和減少編譯時間上,都要求去除這些冗餘。然而讓程序員去分析和去掉這些冗餘,不僅枯燥且不太實際,最重要的是有時候又需要這種冗餘來保證各個模塊的獨立。

為了解決這個問題,上面代碼中的

#ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
/*……………………………*/
#endif /* MONGOOSE_HEADER_INCLUDED */

就起作用了。如果定義了MONGOOSE_HEADER_INCLUDED,#ifndef/#endif之間的內容就被忽略掉。因此,編譯時第一次看到mongoose.h頭文件,它的內容會被讀取且給定MONGOOSE_HEADER_INCLUDED一個值。之後再次看到mongoose.h頭文件時,MONGOOSE_HEADER_INCLUDED就已經定義了,mongoose.h的內容就不會再次被讀取了。

2、extern "C"

首先從字面上分析extern "C",它由兩部分組成——extern關鍵字、"C"。下面我就從這兩個方面來解讀extern "C"的含義。

2.1、extern關鍵字

在一個項目中必須保證函數、變數、枚舉等在所有的源文件中保持一致,除非你指定定義為局部的。首先來一個例子:

1
//file1.c:

2
int x=1;

3
int f(){do something here}

4
//file2.c:

5
extern int x;

6
int f();

7
void g(){x=f();}

在file2.c中g()使用的x和f()是定義在file1.c中的。extern關鍵字表明file2.c中x,僅僅是一個變數的聲明,其並不是在定義變數x,並未為x分配內存空間。變數x在所有模塊中作為一種全局變數只能被定義一次,否則會出現連接錯誤。但是可以聲明多次,且聲明必須保證類型一致,如:

1
//file1.c:

2
int x=1;

3
int b=1;

4
extern c;

5
//file2.c:

6
int x;// x equals to default of int type 0

7
int f();

8
extern double b;

9
extern int c;

在這段代碼中存在著這樣的三個錯誤:

x被定義了兩次
b兩次被聲明為不同的類型
c被聲明了兩次,但卻沒有定義

回到extern關鍵字,extern是C/C++語言中表明函數和全局變數作用范圍(可見性)的關鍵字,該關鍵字告訴編譯器,其聲明的函數和變數可以在本模塊或其它模塊中使用。通常,在模塊的頭文件中對本模塊提供給其它模塊引用的函數和全局變數以關鍵字extern聲明。例如,如果模塊B欲引用該模塊A中定義的全局變數和函數時只需包含模塊A的頭文件即可。這樣,模塊B中調用模塊A中的函數時,在編譯階段,模塊B雖然找不到該函數,但是並不會報錯;它會在連接階段中從模塊A編譯生成的目標代碼中找到此函數。

與extern對應的關鍵字是 static,被它修飾的全局變數和函數只能在本模塊中使用。因此,一個函數或變數只可能被本模塊使用時,其不可能被extern 「C」修飾。

2.2、"C"

典型的,一個C++程序包含其它語言編寫的部分代碼。類似的,C++編寫的代碼片段可能被使用在其它語言編寫的代碼中。不同語言編寫的代碼互相調用是困難的,甚至是同一種編寫的代碼但不同的編譯器編譯的代碼。例如,不同語言和同種語言的不同實現可能會在注冊變數保持參數和參數在棧上的布局,這個方面不一樣。

為了使它們遵守統一規則,可以使用extern指定一個編譯和連接規約。例如,聲明C和C++標准庫函數strcyp(),並指定它應該根據C的編譯和連接規約來鏈接:

1
extern "C" char* strcpy(char*,const char*);

注意它與下面的聲明的不同之處:

1
extern char* strcpy(char*,const char*);

下面的這個聲明僅表示在連接的時候調用strcpy()。

extern "C"指令非常有用,因為C和C++的近親關系。注意:extern "C"指令中的C,表示的一種編譯和連接規約,而不是一種語言。C表示符合C語言的編譯和連接規約的任何語言,如Fortran、assembler等。

還有要說明的是,extern "C"指令僅指定編譯和連接規約,但不影響語義。例如在函數聲明中,指定了extern "C",仍然要遵守C++的類型檢測、參數轉換規則。

再看下面的一個例子,為了聲明一個變數而不是定義一個變數,你必須在聲明時指定extern關鍵字,但是當你又加上了"C",它不會改變語義,但是會改變它的編譯和連接方式。

如果你有很多語言要加上extern "C",你可以將它們放到extern "C"{ }中。

2.3、小結extern "C"

通過上面兩節的分析,我們知道extern "C"的真實目的是實現類C和C++的混合編程。在C++源文件中的語句前面加上extern "C",表明它按照類C的編譯和連接規約來編譯和連接,而不是C++的編譯的連接規約。這樣在類C的代碼中就可以調用C++的函數or變數等。(註:我在這里所說的類C,代表的是跟C語言的編譯和連接方式一致的所有語言)

3、C和C++互相調用

我們既然知道extern "C"是實現的類C和C++的混合編程。下面我們就分別介紹如何在C++中調用C的代碼、C中調用C++的代碼。首先要明白C和C++互相調用,你得知道它們之間的編譯和連接差異,及如何利用extern "C"來實現相互調用。

3.1、C++的編譯和連接

C++是一個面向對象語言(雖不是純粹的面向對象語言),它支持函數的重載,重載這個特性給我們帶來了很大的便利。為了支持函數重載的這個特性,C++編譯器實際上將下面這些重載函數:

1
void print(int i);

2
void print(char c);

3
void print(float f);

4
void print(char* s);

編譯為:

1
_print_int

2
_print_char

3
_print_float

4
_pirnt_string

這樣的函數名,來唯一標識每個函數。註:不同的編譯器實現可能不一樣,但是都是利用這種機制。所以當連接是調用print(3)時,它會去查找_print_int(3)這樣的函數。下面說個題外話,正是因為這點,重載被認為不是多態,多態是運行時動態綁定(「一種介面多種實現」),如果硬要認為重載是多態,它頂多是編譯時「多態」。

C++中的變數,編譯也類似,如全局變數可能編譯g_xx,類變數編譯為c_xx等。連接是也是按照這種機制去查找相應的變數。

3.2、C的編譯和連接

C語言中並沒有重載和類這些特性,故並不像C++那樣print(int
i),會被編譯為_print_int,而是直接編譯為_print等。因此如果直接在C++中調用C的函數會失敗,因為連接是調用C中的print(3)時,它會去找_print_int(3)。因此extern
"C"的作用就體現出來了。

3.3、C++中調用C的代碼

假設一個C的頭文件cHeader.h中包含一個函數print(int i),為了在C++中能夠調用它,必須要加上extern關鍵字(原因在extern關鍵字那節已經介紹)。它的代碼如下:

1
#ifndef C_HEADER

2
#define C_HEADER

3

4
extern void print(int i);

5

6
#endif C_HEADER

相對應的實現文件為cHeader.c的代碼為:

1
#include <stdio.h>

2
#include "cHeader.h"

3
void print(int i)

4
{

5
printf("cHeader %d\n",i);

6
}

現在C++的代碼文件C++.cpp中引用C中的print(int i)函數:

1
extern "C"{

2
#include "cHeader.h"

3
}

4

5
int main(int argc,char** argv)

6
{

7
print(3);

8
return 0;

9
}

執行程序輸出:

3.4、C中調用C++的代碼

現在換成在C中調用C++的代碼,這與在C++中調用C的代碼有所不同。如下在cppHeader.h頭文件中定義了下面的代碼:

1
#ifndef CPP_HEADER

2
#define CPP_HEADER

3

4
extern "C" void print(int i);

5

6
#endif CPP_HEADER

相應的實現文件cppHeader.cpp文件中代碼如下:

1
#include "cppHeader.h"

2

3
#include <iostream>

4
using namespace std;

5
void print(int i)

6
{

7
cout<<"cppHeader "<<i<<endl;

8
}

在C的代碼文件c.c中調用print函數:

1
extern void print(int i);

2
int main(int argc,char** argv)

3
{

4
print(3);

5
return 0;

6
}

注意在C的代碼文件中直接#include "cppHeader.h"頭文件,編譯出錯。而且如果不加extern int print(int i)編譯也會出錯。

4、C和C++混合調用特別之處函數指針

當我們C和C++混合編程時,有時候會用一種語言定義函數指針,而在應用中將函數指針指向另一中語言定義的函數。如果C和C++共享同一中編譯和連接、函數調用機制,這樣做是可以的。然而,這樣的通用機制,通常不然假定它存在,因此我們必須小心地確保函數以期望的方式調用。

而且當指定一個函數指針的編譯和連接方式時,函數的所有類型,包括函數名、函數引入的變數也按照指定的方式編譯和連接。如下例:

01
typedef int (*FT) (const void* ,const void*);//style of C++

02

03
extern "C"{

04
typedef int (*CFT) (const void*,const void*);//style of C

05
void qsort(void* p,size_t n,size_t sz,CFT cmp);//style of C

06
}

07

08
void isort(void* p,size_t n,size_t sz,FT cmp);//style of C++

09
void xsort(void* p,size_t n,size_t sz,CFT cmp);//style of C

10

11
//style of C

12
extern "C" void ysort(void* p,size_t n,size_t sz,FT cmp);

13

14
int compare(const void*,const void*);//style of C++

15
extern "C" ccomp(const void*,const void*);//style of C

16

17
void f(char* v,int sz)

18
{

19
//error,as qsort is style of C

20
//but compare is style of C++

21
qsort(v,sz,1,&compare);

22
qsort(v,sz,1,&ccomp);//ok

23

24
isort(v,sz,1,&compare);//ok

25
//error,as isort is style of C++

26
//but ccomp is style of C

27
isort(v,sz,1,&ccopm);

28
}

注意:typedef int (*FT) (const void* ,const void*),表示定義了一個函數指針的別名FT,這種函數指針指向的函數有這樣的特徵:返回值為int型、有兩個參數,參數類型可以為任意類型的指針(因為為void*)。

最典型的函數指針的別名的例子是,信號處理函數signal,它的定義如下:

1
typedef void (*HANDLER)(int);

2
HANDLER signal(int ,HANDLER);

上面的代碼定義了信函處理函數signal,它的返回值類型為HANDLER,有兩個參數分別為int、HANDLER。 這樣避免了要這樣定義signal函數:

1
void (*signal (int ,void(*)(int) ))(int)

比較之後可以明顯的體會到typedef的好處。

『貳』 c語言如何用qsort對二維數組排序

在C語言中,二維數組按行存儲,對每一行排序很方便,可以把每一行當成一個一維數運薯組,使用排序函數直接進行排序。

然而對每一列進行排序,就不能直接當成一維數組進行排序。但是仍然可以把第j列a[0...M-1][j]在邏輯上當成一維數組進行排序,下面以使用冒泡排序為例對友腔其排序。

對二維數組按列排序後,進一步展示了如何調用快速排序函數按行進行排序。

程序源碼:


#include<stdio.h>#include<stdlib.h>#defineM3#defineN3//輸出二維數組的函數voidprint(inta[][N]){inti,j;for(i=0;i<M;i++){for(j=0;j<N;j++){printf("%d",a[i][j]);}printf(" ");}}//qsort的cmp函數intcmp(constvoid*a,constvoid*b){return*(int*)a-*(int*)b;}intmain(intargc,char*argv){inta[M][N]={3,2,1,9,8,7,6,5,4};printf("按列排序前的二維數組是: ");print(a);printf(" ");intj;for(j=0;j<N;j++)//對每一列進行升序排序{//對第j列進行排序intm,n;intt;for(m=M-1;m>0;m--){for(n=0;n<m;n++){if(a[n][j]>a[n+1][j]){旁告者t=a[n][j];a[n][j]=a[n+1][j];a[n+1][j]=t;}}}}printf("按列排序後二維數組變為: ");print(a);printf(" ");//對按列排序後的二維數組按行升序排序(調用快速排序函數)inti;for(i=0;i<M;i++){qsort(a[i],N,sizeof(a[i][0]),cmp);}printf("按列排序後再按行排序後二維數組變為: ");print(a);printf(" ");system("pause");return0;}

編譯運行後的結果如下:

『叄』 c庫函數源碼

不是你表達不清,也許只是你根本不想仔細看一睛VC下面目錄的源碼,事實上就是有的。後附其中的qsort.c,以證明所言不虛。

VC的庫是提供源碼的,這東西也不值錢。
X:\Program Files\Microsoft Visual Studio\VCXX\CRT\SRC
注意有些可能本身是用匯編寫的。

/***
*qsort.c - quicksort algorithm; qsort() library function for sorting arrays
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* To implement the qsort() routine for sorting arrays.
*
*******************************************************************************/

#include <cruntime.h>
#include <stdlib.h>
#include <search.h>

/* prototypes for local routines */
static void __cdecl shortsort(char *lo, char *hi, unsigned width,
int (__cdecl *comp)(const void *, const void *));
static void __cdecl swap(char *p, char *q, unsigned int width);

/* this parameter defines the cutoff between using quick sort and
insertion sort for arrays; arrays with lengths shorter or equal to the
below value use insertion sort */

#define CUTOFF 8 /* testing shows that this is good value */

/***
*qsort(base, num, wid, comp) - quicksort function for sorting arrays
*
*Purpose:
* quicksort the array of elements
* side effects: sorts in place
*
*Entry:
* char *base = pointer to base of array
* unsigned num = number of elements in the array
* unsigned width = width in bytes of each array element
* int (*comp)() = pointer to function returning analog of strcmp for
* strings, but supplied by user for comparing the array elements.
* it accepts 2 pointers to elements and returns neg if 1<2, 0 if
* 1=2, pos if 1>2.
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/

/* sort the array between lo and hi (inclusive) */

void __cdecl qsort (
void *base,
unsigned num,
unsigned width,
int (__cdecl *comp)(const void *, const void *)
)
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
unsigned size; /* size of the sub-array */
char *lostk[30], *histk[30];
int stkptr; /* stack for saving sub-array to be processed */

/* Note: the number of stack entries required is no more than
1 + log2(size), so 30 is sufficient for any array */

if (num < 2 || width == 0)
return; /* nothing to do */

stkptr = 0; /* initialize stack */

lo = base;
hi = (char *)base + width * (num-1); /* initialize limits */

/* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
prserved, locals aren't, so we preserve stuff on the stack */
recurse:

size = (hi - lo) / width + 1; /* number of el's to sort */

/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
shortsort(lo, hi, width, comp);
}
else {
/* First we pick a partititioning element. The efficiency of the
algorithm demands that we find one that is approximately the
median of the values, but also that we select one fast. Using
the first one proces bad performace if the array is already
sorted, so we use the middle one, which would require a very
wierdly arranged array for worst case performance. Testing shows
that a median-of-three algorithm does not, in general, increase
performance. */

mid = lo + (size / 2) * width; /* find middle element */
swap(mid, lo, width); /* swap it to beginning of array */

/* We now wish to partition the array into three pieces, one
consisiting of elements <= partition element, one of elements
equal to the parition element, and one of element >= to it. This
is done below; comments indicate conditions established at every
step. */

loguy = lo;
higuy = hi + width;

/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi + 1,
A[i] <= A[lo] for lo <= i <= loguy,
A[i] >= A[lo] for higuy <= i <= hi */

do {
loguy += width;
} while (loguy <= hi && comp(loguy, lo) <= 0);

/* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[lo] */

do {
higuy -= width;
} while (higuy > lo && comp(higuy, lo) >= 0);

/* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
either higuy <= lo or A[higuy] < A[lo] */

if (higuy < loguy)
break;

/* if loguy > hi or higuy <= lo, then we would have exited, so
A[loguy] > A[lo], A[higuy] < A[lo],
loguy < hi, highy > lo */

swap(loguy, higuy, width);

/* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
of loop is re-established */
}

/* A[i] >= A[lo] for higuy < i <= hi,
A[i] <= A[lo] for lo <= i < loguy,
higuy < loguy, lo <= higuy <= hi
implying:
A[i] >= A[lo] for loguy <= i <= hi,
A[i] <= A[lo] for lo <= i <= higuy,
A[i] = A[lo] for higuy < i < loguy */

swap(lo, higuy, width); /* put partition element in place */

/* OK, now we have the following:
A[i] >= A[higuy] for loguy <= i <= hi,
A[i] <= A[higuy] for lo <= i < higuy
A[i] = A[lo] for higuy <= i < loguy */

/* We've finished the partition, now we want to sort the subarrays
[lo, higuy-1] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/

if ( higuy - 1 - lo >= hi - loguy ) {
if (lo + width < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy - width;
++stkptr;
} /* save big recursion for later */

if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}

if (lo + width < higuy) {
hi = higuy - width;
goto recurse; /* do small recursion */
}
}
}

/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */

--stkptr;
if (stkptr >= 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}

/***
*shortsort(hi, lo, width, comp) - insertion sort for sorting short arrays
*
*Purpose:
* sorts the sub-array of elements between lo and hi (inclusive)
* side effects: sorts in place
* assumes that lo < hi
*
*Entry:
* char *lo = pointer to low element to sort
* char *hi = pointer to high element to sort
* unsigned width = width in bytes of each array element
* int (*comp)() = pointer to function returning analog of strcmp for
* strings, but supplied by user for comparing the array elements.
* it accepts 2 pointers to elements and returns neg if 1<2, 0 if
* 1=2, pos if 1>2.
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/

static void __cdecl shortsort (
char *lo,
char *hi,
unsigned width,
int (__cdecl *comp)(const void *, const void *)
)
{
char *p, *max;

/* Note: in assertions below, i and j are alway inside original bound of
array to sort. */

while (hi > lo) {
/* A[i] <= A[j] for i <= j, j > hi */
max = lo;
for (p = lo+width; p <= hi; p += width) {
/* A[i] <= A[max] for lo <= i < p */
if (comp(p, max) > 0) {
max = p;
}
/* A[i] <= A[max] for lo <= i <= p */
}

/* A[i] <= A[max] for lo <= i <= hi */

swap(max, hi, width);

/* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi */

hi -= width;

/* A[i] <= A[j] for i <= j, j > hi, loop top condition established */
}
/* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
so array is sorted */
}

/***
*swap(a, b, width) - swap two elements
*
*Purpose:
* swaps the two array elements of size width
*
*Entry:
* char *a, *b = pointer to two elements to swap
* unsigned width = width in bytes of each array element
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/

static void __cdecl swap (
char *a,
char *b,
unsigned width
)
{
char tmp;

if ( a != b )
/* Do the swap one character at a time to avoid potential alignment
problems. */
while ( width-- ) {
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}

『肆』 qsort源碼在linux哪個目錄

linux沒有這個源碼的。
樓碼備主遇到的這個問題,可能不是太准確,遲吵毀 最好是可以幫忙樓主看碰此看
能否私聊呢

『伍』 C++求助,摘花生問題

acm?....
貌似poj上有改滲橘這道題。。
http://acm.pku.e.cn/JudgeOnline/problem?id=1928

講解來看看poj的論壇吧喊橋
http://acm.pku.e.cn/JudgeOnline/bbs?problem_id=1928

我的源碼:

#include<stdio.h>
#include<stdlib.h>

int w,h,t;

struct s_cell
{
int x;
int y;
int num;
}map[52*52];

void putin(void);
void sort(void);
int my_sort(const void* e1,const void *e2);
int count(void);
int getneeded(int mp);
int getfact(int mp);

int main(void)
{
int times;
scanf("%d",×);

for(int i=0;i<times;i++)
{
scanf("%d%d%d",&h,&w,&t);
putin();
sort();

int num=count();
printf("%d\n"核團,num);
}

return 0;
}

void putin(void)
{
int i=0;

for(int y=0;y<h;y++)
for(int x=0;x<w;x++,i++)
scanf("%d",&map[i].num),map[i].x=x,map[i].y=y;
}
void sort(void)
{
qsort(map,h*w,sizeof(map[0]),my_sort);
}
int my_sort(const void* e1,const void *e2)
{
return ((s_cell *)e2)->num-((s_cell *)e1)->num;
}

int count(void)
{
int num=0;
int mp=0;

while(1)
{
int needed=getneeded(mp);
if(t<needed)
break;

t-=getfact(mp);
num+=map[mp].num;
if(map[mp].num==0)
break;

mp++;
}
return num;
}
int getneeded(int mp)
{
if(mp==0)
{
return map[0].y+1 + 1 + map[0].y+1;
}
else
{
return abs(map[mp].x-map[mp-1].x)+abs(map[mp].y-map[mp-1].y) + 1 + map[mp].y+1;
}
}

int getfact(int mp)
{
if(mp==0)
{
return map[0].y+1 + 1;
}
else
{
return abs(map[mp].x-map[mp-1].x)+abs(map[mp].y-map[mp-1].y) +1;
}
}

『陸』 C語言庫函數qsort源代碼

void __fileDECL qsort (
void *base,
size_t num,
size_t width,
int (__fileDECL *comp)(const void *, const void *)
)
#endif /* __USE_CONTEXT */
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
size_t size; /* size of the sub-array */
char *lostk[STKSIZ], *histk[STKSIZ];
int stkptr; /* stack for saving sub-array to be processed */

/此或老* validation section */
_VALIDATE_RETURN_VOID(base != NULL || num == 0, EINVAL);
_VALIDATE_RETURN_VOID(width > 0, EINVAL);
_VALIDATE_RETURN_VOID(comp != NULL, EINVAL);

if (num < 2)
return; /* nothing to do */

stkptr = 0; /* initialize stack */

lo = (char *)base;
hi = (char *)base + width * (num-1); /* initialize limits */

/森升* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
preserved, locals aren't, so we preserve stuff on the stack */
recurse:

size = (hi - lo) /團基 width + 1; /* number of el's to sort */

/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
__SHORTSORT(lo, hi, width, comp, context);
}
else {
/* First we pick a partitioning element. The efficiency of the
algorithm demands that we find one that is approximately the median
of the values, but also that we select one fast. We choose the
median of the first, middle, and last elements, to avoid bad
performance in the face of already sorted data, or data that is made
up of multiple sorted runs appended together. Testing shows that a
median-of-three algorithm provides better performance than simply
picking the middle element for the latter case. */

mid = lo + (size / 2) * width; /* find middle element */

/* Sort the first, middle, last elements into order */
if (__COMPARE(context, lo, mid) > 0) {
swap(lo, mid, width);
}
if (__COMPARE(context, lo, hi) > 0) {
swap(lo, hi, width);
}
if (__COMPARE(context, mid, hi) > 0) {
swap(mid, hi, width);
}

/* We now wish to partition the array into three pieces, one consisting
of elements <= partition element, one of elements equal to the
partition element, and one of elements > than it. This is done
below; comments indicate conditions established at every step. */

loguy = lo;
higuy = hi;

/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi,
A[i] <= A[mid] for lo <= i <= loguy,
A[i] > A[mid] for higuy <= i < hi,
A[hi] >= A[mid] */

/* The doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same
value for both pointers. */

if (mid > loguy) {
do {
loguy += width;
} while (loguy < mid && __COMPARE(context, loguy, mid) <= 0);
}
if (mid <= loguy) {
do {
loguy += width;
} while (loguy <= hi && __COMPARE(context, loguy, mid) <= 0);
}

/* lo < loguy <= hi+1, A[i] <= A[mid] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[mid] */

do {
higuy -= width;
} while (higuy > mid && __COMPARE(context, higuy, mid) > 0);

/* lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi,
either higuy == lo or A[higuy] <= A[mid] */

if (higuy < loguy)
break;

/* if loguy > hi or higuy == lo, then we would have exited, so
A[loguy] > A[mid], A[higuy] <= A[mid],
loguy <= hi, higuy > lo */

swap(loguy, higuy, width);

/* If the partition element was moved, follow it. Only need
to check for mid == higuy, since before the swap,
A[loguy] > A[mid] implies loguy != mid. */

if (mid == higuy)
mid = loguy;

/* A[loguy] <= A[mid], A[higuy] > A[mid]; so condition at top
of loop is re-established */
}

/* A[i] <= A[mid] for lo <= i < loguy,
A[i] > A[mid] for higuy < i < hi,
A[hi] >= A[mid]
higuy < loguy
implying:
higuy == loguy-1
or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid] */

/* Find adjacent elements equal to the partition element. The
doubled loop is to avoid calling comp(mid,mid), since some
existing comparison funcs don't work when passed the same value
for both pointers. */

higuy += width;
if (mid < higuy) {
do {
higuy -= width;
} while (higuy > mid && __COMPARE(context, higuy, mid) == 0);
}
if (mid >= higuy) {
do {
higuy -= width;
} while (higuy > lo && __COMPARE(context, higuy, mid) == 0);
}

/* OK, now we have the following:
higuy < loguy
lo <= higuy <= hi
A[i] <= A[mid] for lo <= i <= higuy
A[i] == A[mid] for higuy < i < loguy
A[i] > A[mid] for loguy <= i < hi
A[hi] >= A[mid] */

/* We've finished the partition, now we want to sort the subarrays
[lo, higuy] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/

if ( higuy - lo >= hi - loguy ) {
if (lo < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy;
++stkptr;
} /* save big recursion for later */

if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}

if (lo < higuy) {
hi = higuy;
goto recurse; /* do small recursion */
}
}
}

/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */

--stkptr;
if (stkptr >= 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}

『柒』 C++標准庫中的qsort在哪個文件里

qsort是C標准庫函數,包含在頭文件stdlib.h中。在ISO C++中為std::qsort,包含在文件cstdlib中。
#include<cstdlib>之後喚拍,可以直接調用std::qsort或using namespace std;然後再調用qsort。
注意庫文件只有聲明。具體實現在鏈接庫文件中,視編譯虛飢環境而定(例如Microsoft C Runtime Library的MSVCP60.dll、MSVCP71.dll、差鏈返MSVCP90.dll等)。且只有二進制代碼,沒有源代碼。可以反匯編相關文件得到一些細節。
如果需要源代碼,可以Google「qsort 源碼」查找相關信息。
----
[原創回答團]

熱點內容
我的世界空島伺服器青金石 發布:2024-04-26 20:18:03 瀏覽:649
微信小程序演算法 發布:2024-04-26 20:03:36 瀏覽:974
易語言模板不能靜態編譯 發布:2024-04-26 19:59:02 瀏覽:352
sql注釋語句 發布:2024-04-26 19:58:48 瀏覽:653
sql存儲過程out 發布:2024-04-26 19:33:15 瀏覽:414
struts2訪問方法 發布:2024-04-26 19:11:36 瀏覽:259
外文翻譯android 發布:2024-04-26 19:03:30 瀏覽:92
土金木配置怎麼樣 發布:2024-04-26 18:52:50 瀏覽:611
這台電腦如何訪問另一台電腦伺服器 發布:2024-04-26 18:51:08 瀏覽:628
怎麼快速了解電腦的配置 發布:2024-04-26 18:42:11 瀏覽:998