 sorting | Sololearn: Learn to code for FREE!

+2

# sorting

void sort(float a[], int indx[], int n); int main() { float a= {23,45,67,89,23,90,100,55}; cout<<"Before Sorting: "<<endl; for(int i=0; i<8; i++) { cout<<a[i]<<" , "; } cout<<"\nAfter Sorting: "<<endl; sort(a,3,8); } void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } cout<<"the array is: "<<endl; for(int i=0; i<n; i++) { cout<<a[i]<<" , "; } } what is the wrong in this code?

+4

sort() function signature void sort( float[], int[], int ); Invoked in main() as follows sort( a, 3, 8 ); The second parameter is defined to accept array of `int`, but instead, an `int` was given when calling it.

+3

The C++ standard library already has a sort() function https://www.cplusplus.com/reference/algorithm/sort/ #include <iostream> #include <algorithm> using namespace std; void showarray(float arr[], int sz); int main() { float a= {23,45,67,89,23,90,100,55}; cout<<"Before Sorting: "<<endl; showarray(a, 8); sort(a, a+8); cout<<"\nAfter Sorting: "<<endl; showarray(a, 8); return 0; } void showarray(float arr[], int sz) { for(int i=0; i<sz; i++) { cout<<arr[i]<<" , "; } } https://code.sololearn.com/c26A162a2a3a

+2

Bubble sort is a classic... Put simply Consider swapping 2 nos a and b You need a 3rd variable c The algorithm: a = b b = c c= a

+2

HATEcoding/Ramisa Fariha No, you do not create the index array with the reverse values of the data array. The data array contains float values whereas the index array contains integers. The index array will be used to display the data array indirectly. This means that the value in the index array determines which data array element is to be displayed. At the start of the program the index array is unsorted and represents the natural order of the data array. When the indirect bubble sort is performed the numbers in the index array are rearranged to represent the sorted order of the data values. So the first value in the index array contains the index number of the lowest value in the data array and the last value in the index array contains the index number of the highest value in the data array. Here is the code refactored and heavily commented. https://code.sololearn.com/c3a0a13a12a1 You really need to read through chapter 6 of the book you are working from. All of the questions you have posted are exercises in that chapter and all of the solutions are provided at the end of the chapter.

+2

thank you @Martin Taylor...the solution only has the function but not the code..I was confused about how to write the index array but now it's clear to me..And I've tried to understand from the book but sometimes the way the language is used there seems hard to understand.

+1

@HATEcoding, is this another exercise from "Programming with C++" by John R. Hubbard? "6.20 Rewrite and test the Bubble Sort function presented in Example 6.13 on page 134, as an indirect sort. Instead of moving the actual elements of the array, sort an index array instead." This technique requires the use of a second array to hold the index numbers of the actual data array. You have not created this second array. The indirect sort is a solution used when the array contains large data structures. It is more efficient to move around the index numbers than to move around large data structures. For example if the array myarray contains.. 66, 55, 11, 22, 44, 33 then the array indexarray would contain... 2, 3, 5, 4, 1, 0 because the sort order for myarray is... myarray // myarray[indexarray] myarray // myarray[indexarray] myarray // myarray[indexarray] myarray // myarray[indexarray] myarray // myarray[indexarray] myarray // myarray[indexarray]

+1

Is this some kind of test? ☺️ I counted three errors, one extra operation and a violation of logic ☺️ #include <iostream> using namespace std; // Declare function void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } cout<<"the array is: "<<endl; for(int i=0; i<n; i++) { cout<<a[indx[i]]<<" , ";//3 } } int main() { float a= {23,45,67,89,23,90,100,55}; cout<<"Before Sorting: "<<endl; int indx; //1 for(int i=0; i<8; i++) { indx[i] = i; //2 cout<<a[i]<<" , "; } cout<<"\nAfter Sorting: "<<endl; sort(a,indx,8); } P. S: "sort(a, 3, 8); 🤔 Why three, what were you trying to get?" ☺️

0

@ Martin Taylor will I create the index array with the reverse values of the main array? void sort(float a[], int indx[], int n); void showarray( float a[],int n); void showindxarray( int a[],int n); int main() { float a= {23,45,67,89,23,90,100,55}; int array_size=8; cout<<"Before Sorting: "<<endl; showarray(a,array_size); int indx[]= {55,100,90,23,89,67,45,23}; int indxarray_size=8; cout<<"\nAfter Sorting: "<<endl; sort(a,indx,8); showindxarray(indx,indxarray_size); } void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } } void showarray( float a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } void showindxarray( int a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } after swapping indx array not changing

0