简体   繁体   中英

Why is the number of location swaps showing up as zero?

While I'm pretty sure the number of location swaps for everything else is correct, the one for my InsertionSort function is showing up as zero.

I'm not sure why.

Any ideas on how to fix this logic error?

   #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;
 }

You get 0 swaps for insertion sort because insertion sort performed 0 swaps, because the array was already sorted, because you ran bubble sort on it earlier.

You don't get 0 swaps for selection sort because your selection sort function always performs N-1 swaps, if N is the size of the array, even if the array is already sorted.

You don't get 0 swaps for bubble sort because the array isn't already sorted at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM