2016-08-01 47 views
-1

儘管我很確定所有其他位置交換的次數是正確的,但我的InsertionSort函數的交換次數顯示爲零。爲什麼地點互換的數量顯示爲零?

我不知道爲什麼。

有關如何解決此邏輯錯誤的任何想法?

#include <iostream> 

using namespace std; 

const int SIZE=20; 

void bubbleSort(int numbers[], int SIZE); 
void selectionSort(int numbers[], int SIZE); 
void insertionSort(int numbers[], int SIZE, int &a, int &b); 



int main() 
{ 
    int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51}; 
    int value=0; 
    bool found; 
    int a; 
    int b; 

    cout << "Today we are going to be searching for values." << endl; 
    cout << "These are the values you have to choose from" << endl; 

    for (int i=0; i<SIZE; i++) 
     cout << numbers[i]<<"; "; 

    do 
    { 
     cout << "Make sure to enter a value that's in the list." << endl; 
     cin >> value; 
     found=false; 
     for (int i=0; i<SIZE; i++) 
     { 
      if (value==numbers[i]) 
      { 
       found=true; 
       break; 
      } 
     } 
     if (!found) 
      cout << "Enter a valid value !" << endl; 
    } 
    while (!found); 

    bubbleSort(numbers, SIZE); 
    selectionSort(numbers, SIZE); 
    insertionSort(numbers, SIZE, a, b); 





    return 0; 
} 

void bubbleSort (int numbers[], int SIZE) 
{ 
    cout<<"\nOriginal order: "; 
    for(int i=0;i<SIZE;i++) 
    cout<<numbers[i]<<' '; 
    int maxElement; 
    int index,counter=0; 

    for(maxElement=SIZE-1; maxElement>=0; maxElement--) 
    { 
     for(index=0;index<=maxElement-1;index++) 
     { 
      if(numbers[index]>numbers[index+1]) 
      { 
       swap(numbers[index], numbers[index+1]); 
       counter++;//increments counter everytime swap occurs 
      } 
     } 
    } 
cout<<"\nBubble Sorted: "; 
    for(int i=0;i<SIZE;i++) 
    cout<<numbers[i]<<' '; 
    cout<<"\nNumbers of location swap: "<<counter<<endl; 
} 

void swap(int &a, int &b) 
{ 
    int temp; 
    temp=a; 
    a=b; 
    b=temp; 
} 

void selectionSort(int numbers[], int SIZE) 
{ cout<<"\nOriginal order: "; 
    for(int i=0;i<SIZE;i++) 
    cout<<numbers[i]<<' '; 
    int startScan; 
    int index; 
    int miniIndex; 
    int miniValue; 
    int counter=0; 

    for(startScan=0;startScan<(SIZE-1);startScan++) 
    { 
     miniIndex=startScan; 
     miniValue=numbers[startScan]; 

     for(index=startScan+1;index<SIZE;index++) 
     { 
      if(numbers[index]<miniValue) 
      { 
       miniValue=numbers[index]; 
       miniIndex=index; 
      } 

     } 
     swap(numbers[miniIndex], numbers[startScan]); 
     counter++; 
    } 
cout<<"\nSelection Sorted: "; 
    for(int i=0;i<SIZE;i++) 
    cout<<numbers[i]<<' '; 
    cout<<"\nNumbers of location swap: "<<counter<<endl; 
    cout << endl; 
} 

void insertionSort(int numbers[], int SIZE, int &a, int &b) 
    { 
    int temp = a; a = b; b = temp; 
    int j, swap = 0; 
    cout<<"Original order: "; 
    for(int i = 0; i < SIZE; i++) 
    cout<< numbers[i] << ' '; 
    for (int i = 0; i < SIZE; i++){ 
     j = i; 

     while (j > 0 && numbers[j] < numbers[j-1]) 
      { 
       temp = numbers[j]; 
       numbers[j] = numbers[j-1]; 
       numbers[j-1] = temp; 
       j--; swap++; 
       } 
     } 
     cout <<"\nThe number of location swaps is: "<< swap << endl; 
     return; 
} 
+1

也許是因爲數組已經排序,因爲您只是對它排序了兩次。 – immibis

+0

我不確定你在說什麼。 @immibis –

+2

當數組尚未排序時,插入排序只進行交換。由於數組已經排序,因此它進行0次交換。 – immibis

回答

2

你得到0掉期插入排序,因爲插入排序進行0互換,因爲數組已經排序,因爲你跑冒泡排序就可以更早。

因爲如果N是數組大小,即使數組已經排序,您的選擇排序函數也總是執行N-1交換,所以您不會獲得0交換。

對於氣泡排序,您不會獲得0次交換,因爲該數組尚未在此點排序。

相關問題