I don't understand the mechanism of the source code of c language bubble sort
I don't understand c language bubble sort at all
I found out how to do bubble sort on the web, but the problem is that the source code of bubble sort in c language
I can't figure out what's going on
Even if you understand bubble sort, it doesn't make sense if you don't understand the mechanism of bubble sort in the source code, so lol
By the way, it's a problem
Read the program description below and enter the appropriate words to put in the numbers in the program
The purpose of this program is to sort the 10 input integer values in ascending order and output them to the screen, and then
Checks whether the specified search target numerical value exists in the previous 10 numerical values, and displays the result on the screen.
Is to display.
It should be noted that the numerical value to be input is an integer of 0 or more and 100 or less, and all the values at the time of input are correct.
① Enter 10 numbers from the screen
(2) Sort the 10 input numbers in ascending order by bubble sort and output to the screen.
③ Enter the search target numerical value
(4) Check by 2-minute search whether the specified search target numerical value exists in the 10 numerical values output in (2).
, If it exists, "found" If it does not exist, "not found" is output to the screen.
⑤ Repeat the processes of ③ and ④ until a negative number is specified for the search target numerical value.
<< Execution example >>
Please enter 10 numbers
20 3 12 8 18 6 32 25 19 30
3 6 8 12 18 19 20 25 30 32
Please enter the value to be searched
12
Found
Please enter the value to be searched
13
Could not be located
Please enter the value to be searched
2
Could not be located
Please enter the value to be searched
32
Found
Please enter the value to be searched
−1
is
The answer number is
❸❸ is data [j]> data [j + 1]
❸❹ is data [j] = data [j + 1]
❸❺ is data [middle] == target
❸❻ is middle +1
❸❼ is low> high
is,
Can you tell me how to write in detail if you like?
Thank you
code
include<stdio.h>
define N 10
int main(void)
{
int data[N], target;
int i,j,work;
int low, high, middle;
/ * Numerical input / *
printf ("Enter 10 numbers. \ N");
for (i=0;i<N;i++)
scanf("%d",&data[i]);
/Alignment/
for(i=0;i<N;-1;i++){
for(j=N-2;j>=i;j--){
if( ❸❸ ){
work = data[j];
❹❹ ;
data[j+1]=work;
}
}
}
/ * Enter the sorted numbers / *
for(i=0;i<N;i++)
printf("%d",data[i]);
printf("¥n");
/ * Numerical search / *
while(1){
printf ("Please enter the value to be searched \ n");
scanf("%d",&target);
if(target < 0)
break;
low=0;
high =N-1;
while(low<= high){
middle = (low + high) /2;
if( ❸❺ )
break;
else if (data[middle]< target)
low = ❸❻ ;
else
high = middle -1;
}
if( ❸❼ )
printf ("not found \ n");
else
printf ("found \ n");
}
return 0;
}