Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Exercise) Create-sortArray2.cpp In this part of the lab, you will write a progr

ID: 3668590 • Letter: #

Question

(Exercise) Create-sortArray2.cpp In this part of the lab, you will write a program that uses a different implementation of selection sort from the one you chose before: if you had chosen to search for the minimum for the traverse of the array, now choose the maximum, or . if you had chosen to search for the maximum for the traverse of the array, now choose the minimum. You may choose whether to traverse the array forward or backward. Also, this time you are requested to calculate the number of swaps used to complete sorting the array. You can write a program starting from the previous one, and modify it to sort the array using another selection sort implementation. Once the array is sorted, output first to the console if the output is sorted in ascending or descending order (depending on what you decided to use):

Explanation / Answer

Selection sort is the best sorting algorithm if you need less no of swaps for your input.

CASE 1: let us take the minmum element (ascending order case) and the calculation of no of swaps.

I used variables i,j and temp for swapping and used min_num for comparison with minimum number.

CODE:

#include <stdio.h>

void swap(int *x, int *y)

{

    int temp = *x;

    *x = *y;

    *y = temp;

}

void selectionSort(int arr[], int n)

{

    int i, j, min_num;

    

    for (i = 0; i < n-1; i++)

    {

        

        min_num = i;

        for (j = i+1; j < n; j++)

          if (arr[j] < arr[min_num])

            min_num = j;

        

        swap(&arr[min_num], &arr[i]);

    }

}

void printArray(int arr[], int size)

{

    int i;

    for (i=0; i < size; i++)

        printf("%d ", arr[i]);

    printf(" ");

}

CASE 2: now we take the case where we take the max element and decending order is obtained

CODE:

void selectionSortArray(int Array[], int SIZE, long PerformanceDataArray[])

{

    //Variable declarations for performance testing

    long swapCounter = 0;//Increment this counter whenever a swap takes place

    long comparisonCounter=0;//Increment this counter whenever a comparison takes place

         

    int TempVal = 0;

    int IndexMaxVal = 0;

    int CompResult;

     

    // Now the Selection Sort begins //

    for(int i = SIZE -1; i > 0; i--)

    {

        IndexMaxVal = 0;

         

        for(int j = 1; j <= i; j++)

        {

            CompResult = compare(Array[j], Array[IndexMaxVal]);

            comparisonCounter++;

            if(CompResult > 0)

            {

                IndexMaxVal = j;

            }

        }

        swap(Array[IndexMaxVal], Array[i]);

        swapCounter++;

    }

    PerformanceDataArray[0] = comparisonCounter;

    PerformanceDataArray[1] = swapCounter;

}//End of selectionSortArray()

In this way we can use comparison and swap counters to count no of swaps in ascending and descending order in selection sort.

void selectionSortArray(int Array[], int SIZE, long PerformanceDataArray[])