2011-11-15 29 views
0

我想在鏈表上實現選擇排序。我希望它可以直接鏈接的列表,而不是複製使用節點指針,而不是數組索引的方法,我在這裏粘貼執行:如何執行選擇在鏈接列表上排序?

void listClass::selectionsort(int array[], int size) 
{ 
    int startscan, minIndex, minValue; 

for (startscan = 0; startscan < (size - 1); startscan++) 
{ 
    minIndex = startscan; 
    minValue = array[startscan]; 
    for (int index = startscan + 1; index < size; index++) 
    { 
     if (array[index] < minValue) 
     { 
      minValue = array[index]; 
      minIndex = index; 
     } 
    } 
    array[minIndex] = array[startscan]; 
    array[startscan] = minValue; 
} 
} 

我將如何調整這個函數接受我的鏈接列表?並對其進行分類?我也不想使用任何類型的STL容器。

+0

這是功課嗎? –

+0

否定性,我正在從事面向軟件工程實習的實習面試問題 – Extinct23

回答

1

假設列表是{90,13,5,12}。在開始處開始一個指針。

{* 90,13,5,12}

查找指針後最小的部件,只是指針之前移動它。

{5,* 90,13,12}

查找指針後最小的部件,只是指針之前移動它。

{5,12,* 90,13}

同樣。

{5,12,13,* 90}

同樣。

{5,12,13,90}

指針滲到列表的末尾,我們就大功告成了,列表進行排序。