2012-10-27 46 views
-2

我有一個程序,在該程序中我必須將選擇排序更改爲使用Visual Studio 2010的C++程序中的插入排序。下面的代碼是我所展示的選擇排序:使用C++我需要將選擇排序改爲插入排序

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       int data[] = {5,6,7,1,2,4,7,8,9,11,12,13,0,7,8,5,3,2,6,8}; 
       if (button1->Text == "Start") 
       { 
        panel1->Visible = true; 
        button1->Text = "Sort"; 
        messageLabel->Text="Unsorted Array"; 
        DrawArray(data, 20); 
       } 
       else 
       { 
        if(ascButton->Checked) 
        { 
         selectionSort(data,20,1); 
         messageLabel->Text="Sorted Array - Ascending"; 
        } 
        else 
        { 
         selectionSort(data,20,2); 
         messageLabel->Text="Sorted Array - Descending"; 
        } 
        DrawArray(data, 20); 
       } 
      } 

    private: System::Void selectionSort(int array[], int n,int ascending) 
      { 
       int temp,index; // temporary variable used for swapping and index 
       int i, j; 
       for (i = 0; i < n-1; i++) 
       { 
        index = i; 
        for (j = i+1; j < n; j++) 
        { 
         if(ascending==1) 
         { 
          if (array[j] < array[index]) 
          { 
           index = j; 
          } 
         } 
         else 
         { 
          if (array[j] > array[index]) 
          { 
           index = j; 
          } 
         } 
        } 
        if (index != i) 
        { 
          temp = array[i]; 
          array[i] = array[index]; 
          array[index] = temp; 
        } 
       } // end for 

      } // end method selectionSort 

我已實現了以下變化和程序運行沒有任何錯誤,但數據並不像它被排序。這裏是下面的更新後的代碼:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       int data[] = {5,6,7,1,2,4,7,8,9,11,12,13,0,7,8,5,3,2,6,8}; 
       if (button1->Text == "Start") 
       { 
        panel1->Visible = true; 
        button1->Text = "Sort"; 
        messageLabel->Text="Unsorted Array"; 
        DrawArray(data, 20); 
       } 
       else 
       { 
        if(ascButton->Checked) 
        { 
         insertionSort(data,20,1); 
         messageLabel->Text="Sorted Array - Ascending"; 
        } 
        else 
        { 
         insertionSort(data,20,2); 
         messageLabel->Text="Sorted Array - Descending"; 
        } 
        DrawArray(data, 20); 
       } 
      } 

    private: System::Void insertionSort(int array[], int n,int ascending) 
      { 
       int data[]= {5,6,7,1,2,4,7,8,9,11,12,13,0,7,8,5,3,2,6,8}; 
       int vacant; // Position of last vacated element 
       int temp; // Temporary copy of unsorted value 
       int i; 
       for (i=0; i < n-1; i++) 
       { 
        temp = data[i+1]; // Copy first unsorted value 
        for (vacant = i+1; 
         ((vacant > 0) && (data[vacant-1] > temp)); 
         vacant--) 
        { 
         data[vacant] = data[vacant-1]; // Shift data up 
        } // End inner loop 
       data[vacant] = temp; // Insert value into vacated element 
       } // End outer loop 
      } 
+0

你能不能請你發佈你面臨的問題? – aakash

+0

我並不確定我應該如何改變這種編碼方式。這個程序與選擇排序一樣完美,但我需要嘗試將排序改爲插入排序。 – Rocky

+0

你有沒有理由爲我扣分?我正在問一些問題的一些幫助和指導,我不斷得到扣分?我在這裏做錯了什麼? – Rocky

回答

1

下面的代碼段應改爲​​:

if(ascButton->Checked) 
{ 
    //selectionSort(data,20,1); 
    insertionSort(data, 20, 1); 
    messageLabel->Text="Sorted Array - Ascending"; 
} 
else 
{ 
    //selectionSort(data,20,2); 
    insertionSort(data, 20, 1); 
    messageLabel->Text="Sorted Array - Descending"; 
} 

還宣佈私人:System::Void insertionSort(int array[], int n,int ascending)就像System::Void selectionSort(int array[], int n,int ascending)填寫正確的插入排序的代碼存在。

+1

謝謝你的寶貴意見。我已經對您在此處提到的領域進行了更改,並且在上面的帖子中更新了此信息。再次感謝您回覆我原來的帖子。 – Rocky