2012-12-04 66 views
3

我需要了解稱爲珠粒排序的算法。我找到了算法的C++實現,但是我沒有完全理解它。 我什麼混淆的最多的是在這條線會發生什麼:什麼List [i] ++;應該這樣做?

List[i]++; 

幫助我,請

下面是完整的代碼:

#include <iostream> 
#include <vector> 

using std::cout; 
using std::vector; 

void distribute(int dist, vector<int> &List) { 
    //*beads* go down into different buckets using gravity (addition). 
    if (dist > List.size()) 
     List.resize(dist); //resize if too big for current vector 

    for (int i=0; i < dist; i++) 
     List[i]++; 
} 

vector<int> beadSort(int *myints, int n) { 
    vector<int> list, list2, fifth (myints, myints + n); 

    cout << "#1 Beads falling down: "; 
    for (int i=0; i < fifth.size(); i++) 
     distribute (fifth[i], list); 
    cout << '\n'; 

    cout << "\nBeads on their sides: "; 
    for (int i=0; i < list.size(); i++) 
     cout << " " << list[i]; 
    cout << '\n'; 

    //second part 

    cout << "#2 Beads right side up: "; 
    for (int i=0; i < list.size(); i++) 
     distribute (list[i], list2); 
    cout << '\n'; 

    return list2; 
} 

int main() { 
    int myints[] = {734,3,1,24,324,324,32,432,42,3,4,1,1}; 
    vector<int> sorted = beadSort(myints, sizeof(myints)/sizeof(int)); 
    cout << "Sorted list/array: "; 
    for(unsigned int i=0; i<sorted.size(); i++) 
     cout << sorted[i] << ' '; 
} 
+0

你對這個問題得到了正確的答案嗎?你認爲任何一個都是正確的嗎? –

回答

1

很簡單,就是訪問元素在vector<int>參數您的功能(稱爲List)索引爲i,並將其值遞增1

3

它將您列表中的第i個元素的值遞增1.