I did write code to count the number of swaps needed to sort an array in higher to lower order. In my code, while loops result is unexpected. I wanted to continue the loop until I get the highest value in the arr[0]
position and the lowest value in the arr[n-1]
position.
Here is my code
#include <iostream>
using namespace std;
int main(){
int n,high, low, swap_time=0;
cin >> n;
int arr[n]={};
for(int i=0; i<n; i++){
cin >> arr[i];
}
high = arr[0];
low = arr[0];
for(int i=0; i<n; i++){
if(high<arr[i]){
high = arr[i];
}
if(low>arr[i]){
low = arr[i];
}
}
while(arr[0] != high && arr[n-1] != low){ //WE ARE TALKING ABOUT THIS PART - start
for(int i=0; i<n; i++){
if(i < n-1){
int temp;
if(arr[i] < arr[i+1]){
cout << "Array ==> ";//This is for showing array - start
for(int j=0; j<n; j++){
cout << arr[j] << " ";
}
cout << endl;//This is for showing array - end
temp = arr[i];//swap part - start
arr[i] = arr[i+1];
arr[i+1] = temp;//swap part - end
swap_time += 1;//counting swap
break;
}
}
}
}//WE ARE TALKING ABOUT THIS PART - end
cout << "We are Outside of while loop!" << endl;
cout << "High ==> " << high << endl;
cout << "Low ==> " << low << endl;
cout << "Final Array ==> ";
for(int j=0; j<n; j++){
cout << arr[j] << " ";
}
cout << endl;
cout << "Number of Swap Time ==> " << swap_time << endl;
return 0;
}
My input is (size of array and array),
7
10 10 58 31 63 40 76
Here is my code output
7
10 10 58 31 63 40 76
Array ==> 10 10 58 31 63 40 76
Array ==> 10 58 10 31 63 40 76
Array ==> 58 10 10 31 63 40 76
Array ==> 58 10 31 10 63 40 76
Array ==> 58 31 10 10 63 40 76
Array ==> 58 31 10 63 10 40 76
Array ==> 58 31 63 10 10 40 76
Array ==> 58 63 31 10 10 40 76
Array ==> 63 58 31 10 10 40 76
Array ==> 63 58 31 10 40 10 76
Array ==> 63 58 31 40 10 10 76
Array ==> 63 58 40 31 10 10 76
We are Outside of while loop!
High ==> 76
Low ==> 10
Final Array ==> 63 58 40 31 10 76 10
Number of Swap Time ==> 12
Process returned 0 (0x0) execution time : 1.006 s
Press any key to continue.
Here is my expectation
7
10 10 58 31 63 40 76
Array ==> 10 10 58 31 63 40 76
Array ==> 10 58 10 31 63 40 76
Array ==> 58 10 10 31 63 40 76
Array ==> 58 10 31 10 63 40 76
Array ==> 58 31 10 10 63 40 76
Array ==> 58 31 10 63 10 40 76
Array ==> 58 31 63 10 10 40 76
Array ==> 58 63 31 10 10 40 76
Array ==> 63 58 31 10 40 10 76
Array ==> 63 58 31 40 10 10 76
Array ==> 63 58 40 31 10 10 76
Array ==> 63 58 40 31 10 76 10
Array ==> 63 58 40 31 76 10 10
Array ==> 63 58 40 76 31 10 10
Array ==> 63 58 76 40 31 10 10
Array ==> 63 76 58 40 31 10 10
We are Outside of while loop!
High ==> 76
Low ==> 10
Final Array ==> 76 63 58 40 31 10 10
Number of Swap Time ==> 17
Process returned 0 (0x0) execution time : 1.006 s
Press any key to continue.
I don’t know why this while loop break when swap_time
is 12.
According to my code output, high = 76
and low = 10
. But when it’s broken this time arr[0]
was 63( which is not equal to high
). That means the expression inside while loop was true. So, why it’s breaking, please let me know where I misunderstood?