Bubble sort is a simple sorting algorithm that compares elements of order 22 to each other.
If it is from the big to the small order, then the two elements compare each other, the big one will be in front;
Instead, it will be in the back. Bubble sorting is categorized from large to small and sorted from smallest to largest .
Code Implementation section:
From big to small
1: Declaring an array
int nums[]={1,3,-8,2,,4,5};
2: Loop through the length of the array, and determine if the previous value is less than the last value, then the largest swap to the final face
for (int i=0;i<nums.length;i++) {
for (int j=i+1,j<nums.length,j++) {
If the previous value is less than the next value, replace the large value with the back.
if (Nums[i]<nums[j]) {
int temp=nums[i];
NUMS[I]=NUMS[J];
Nums[j]=temp;
}
}
}
Take out the ordered values.
for (int i=0;i<nums.length;i++) {
System.out.print (Nums[i]);
}
From small to large
And from the big to the small difference is the small change to the front, big row after.
1: Declaring an array
int nums[]={1,3,-8,2,,4,5};
2: Loop through the length of the array and determine if the previous value is less than the last value, then switch the small one to the final face.
for (int i=0,i<nums.length-1,i++) {
If the previous value is less than the last value, replace the small value with the back
if (Nums[i]>nums[i+1]) {
int temp=nums[i];
NUMS[I]=NUMS[I+1];
Nums[i+1]=temp;
}
}
Take out the ordered values.
for (int i=0;i<nums.length;i++) {
System.out.print (Nums[i]);
}
Talking about bubble sort