Home > Back-end >  About the bubble sort for loop
About the bubble sort for loop

Time:09-28

When doing bubble sort, I see a lot of programmers first cycle begins with 0, the first layer of cycle is not representative number back? Confused, therefore, feel easy to make the person produces 0, do not understand, so I just want to start from 1, if I as an array element subscript, n as the number of elements, j said round number, the great god, please look at me this way is correct? And I apply this view in programming development time? The code is as follows:
# define n 10
#include
using namespace std;
Int main ()
{
Int I, j;
99,89,79,69,59,49,39,29,19,9 int a [n]={};
For (j=1; J & lt; N; J++)
{
For (I=0; i {
If (a [I] & gt; A [I + 1])
{
Int swap;
Swap=a, [I].
A [I]=a, [I + 1].
A [I + 1)=swap;
}
}
cout <"The first" & lt; For (I=0; i {

cout }
cout }
return 0;
}
The above code execution results:


The great god, please help to show me the idea is in line with the programming logic and practicability, thank you very much

CodePudding user response:

As long as the cycle number for you from any initial value cycle can be love, so that you deal with no problem,

CodePudding user response:

Need to see is people do not represent the round but from an array subscript 0, so understanding deviation, your approach should accord with starting from the subscript 0 and achieve the subscript to the end of the can, truth is the same, as for how to write a variety of reason must be the same,

CodePudding user response:

Understanding deviation, bubbling cycle the outermost variables (assuming I) represents an array subscript, such as PHP, golang, the first element of the array subscript is 0, so I usually start from zero, so array [I] is the first element, if the first element of the array subscript is 0, I begin from 1, so when you take the first element of the array with array [] I - 1
  • Related