2013-01-17 33 views
-6

我有在C的陣列(2D):請幫幫我上排序在C或C++

80 | 100 | 70 | 50 | 120

甲| C | d | F | K

我需要將其分類到這一點:

50 | 70 | 80 | 100 | 120

F | d | A | C | K

我使用C或C++。請幫幫我。

謝謝。

+1

請提供您已經嘗試了什麼! – billz

+1

在C'qsort'應該是任務。 – cnicutar

+1

這是怎樣的: A | C | D | F | K 正在分類爲 F | D | A | C | K? –

回答

1

在C++:

std::vector<int> keys; 
std::vector<char> values; 
std::map<int, char> kvtable; 

keys.push_back(80); 
values.push_back('A'); 

keys.push_back(100); 
values.push_back('C'); 

keys.push_back(70); 
values.push_back('D'); 

keys.push_back(50); 
values.push_back('F'); 

keys.push_back(120); 
values.push_back('K'); 

int i; 
for (i = 0; i < keys.size(); i++) { 
    kvtable[keys[i]] = values[i]; 
} 

std::sort(keys.begin(), keys.end()); 

for (i = 0; i < keys.size(); i++) { 
    std::cout << keys[i] << " = " << kvtable[keys[i]] << "\n"; 
} 
0

,我認爲你應該做的是創造指針的效用1-d陣列到您的陣列的第一維和提供自定義比較函數的實用程序數組排序。然後,你可以做指針運算創建數組排序的副本(如果需要使用的memcpy覆蓋原來的數組)

static int ARRAY_LEN = 5; 

int[2][ARRAY_LEN] origArray; 
int*[ARRAY_LEN] pointerArray; 

for(int i = 0; i < ARRAY_LEN; i++){ 
    pointerArray[i] = &origArray[0][i]; //create the pointer array 
} 

qsort(pointerArray, ARRAY_LEN, sizeof(int*), compar); //sort the pointer array 

/*We need to define the comparison function */ 
int compar (const void * elem1, const void * elem2){ 
    return **(int**)elem1 - **(int**)elem2; 
} 

int[2][ARRAY_LEN] sortedArray; 

for(int i = 0; i < ARRAY_LEN; i++){ 
    int* curr = pointerArray[i]; 
    int firstDimValue = *curr; // get the first dimension of the ith element of the sorted array 
    int secondDimValue = curr[ARRAY_LEN]; // get the 2nd dimension of the ith element of the sorted array - this works because of the way that 2D arrays are laid out in memory 

    sortedArray[0][i] = firstDimValue; 
    sortedArray[1][i] = secondDimValue; 
} 
+0

'int 2nd_dimValue'?真?是時候啓動處理標識符的標準部分了。 – 2013-01-17 05:39:20

+1

好的 - 我們快樂嗎?我加了足夠的[OH-]來抵消你嗎?哈哈 – Cam

+0

很酷。當然:P – 2013-01-17 05:50:00

0
  1. 創建一個數組來保存這些數字是這樣的:

    INT array [5] = {80,100,70,50,120};

2.創建一個函數比較兩個elems的:

bool compare(int a,int b) 
{ 
    return a<b; //ASC 
} 

3.使用庫函數排序():

sort(array,array+5,compare); 

4.Now陣列已經由ASC已排序正確。

0

而且有這麼多種類的排序是這樣的:

void Swap(int &x,int &y) //not sort, just for swap two elems 
{ 
    int temp; 
    temp=x; 
    x=y; 
    y=temp; 
} 

void StraightInsertSort(int elem[],int n) 
{ 
    int i,j,temp; 
    for(i=1;i<n;i++) 
    { 
     temp=elem[i]; 
     for(j=i-1;j>=0&&temp>elem[j];j--) 
     { 
      elem[j+1]=elem[j]; 
     } 
     elem[j+1]=temp; 
    } 
} 

void SimpleSelectSort(int elem[],int n) 
{ 
    for(int i=0;i<n-1;i++) 
    { 
     int lowIndex=i; 
     for(int j=i+1;j<n;j++) 
     { 
      if(elem[j]>elem[lowIndex]) 
      lowIndex=j; 
     } 
    Swap(elem[i],elem[lowIndex]); 
    } 
} 

void Bubblesort(int elem[],int n) 
{ 
    for(int i=n-1;i>0;i--) 
    { 
     for(int j=0;j<i;j++) 
     { 
      if(elem[j]<elem[j+1]) 
      { 
       Swap(elem[j],elem[j+1]); 
      } 
     } 
    } 
}