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

i would like to highlight is that there may be similar elements in an array and

ID: 3552241 • Letter: I

Question

i would like to highlight is that there may be similar elements in an array and therefore more than 1 indexs could be returned for 1st,2nd,3rd or nth smallest value of an array.






Write a function simpleSort to find the smallest value of an array (using the function findMin), print out that value and the indices of the array elements taking that value; then find the second smallest value (using the function findNextMin), print out that value and the indices of the array elements taking that value; then find the third smallest ... ; .... ; until find the largest value (using the function findNextMin), print out that value and the indices of the elements taking that value. If the function findNextMin returns false, you terminate this function (i.e., the function in Part 3). The function declaration is given as (the second parameter is the array size):void simpleSort(double [], int);

Explanation / Answer

#include <stdio.h>

#include <iostream>


using namespace std;


int *mark; // used whehter the elemnts is already found or not

void findMin(double A[], int n){

int i;

int minIndex=0;

for(i=0; i<n; i++){

if(A[i]<A[minIndex]){

minIndex=i;

}

}

cout<<A[minIndex]<<" "<<minIndex<<endl;

mark[minIndex]=1;

}

int findNextMin(double A[], int n){

int i, nextMinI=0;

for(i=0; i<n;i++){

if(mark[i] ==0){

nextMinI=i;

break;

}

}

for(i=0; i<n;i++){

if(A[i]< A[nextMinI] && mark[i] ==0)

nextMinI = i;

}

if(mark[nextMinI] == 0){

cout<<A[nextMinI]<<" "<<nextMinI<<endl;

mark[nextMinI] = 1;

return 1;

}

else

return 0;

}



void simplesort(double A[], int n){

mark = new int[n];

int i;

for(i=0; i<n; i++){

mark[i] =0;

}

findMin(A, n);

while(findNextMin(A, n));

}

int main(void) {

// your code goes here

int n;

cin>>n;

int i;

double A[n];

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

cin>>A[i];

simplesort(A, n);

return 0;

}