C++ 气泡排序不使用此算法对最后一个数字进行排序
我有问题,这个代码中的最后一个数字没有被排序C++ 气泡排序不使用此算法对最后一个数字进行排序,c++,bubble-sort,C++,Bubble Sort,我有问题,这个代码中的最后一个数字没有被排序 // This is the more advanced optimzed version of bubble sort int modifiedBubbleSortArray(int array[]) { int swapped = 0; do { swapped = false; // We specify a loop h
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
//这是冒泡排序更高级的优化版本
int-modifiedBubbleSortArray(int数组[])
{
整数交换=0;
做{
交换=假;
//我们在这里为排序指定一个循环
对于(int i=0;i而言,内环应为:
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
if (array[j]>array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
}
试一试,看看冒泡排序的变体是否正确排序
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
要按降序排序,只需更改if条件:
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
if (array[j]<array[j+1])
if(array[j]-快速排序的一种变体。算法更复杂,但排序也更有效,是O(N logn)而不是冒泡排序的复杂性O(N^2)。亲爱的朋友(神秘主义者)
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
下面的代码是用java编写的,
但这是BubbleSort算法的完美例子
// This is the more advanced optimzed version of bubble sort
int modifiedBubbleSortArray(int array[])
{
int swapped = 0;
do {
swapped = false;
// We specify a loop here for the sorting
for(int i=0;i<NUM_ARRAYS;i++)
{
// We specify aother loop here for the sorting
for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
{
// If the array i is better than j we enter this swap
if (array[j]<array[j+1])
{
// We swap here for the functions
swap(array[j], array[j+1]);
// We measure the amount of times we swap
amountOfSwaps += 1;
// If we swapped we break out of the loop
swapped = true;
}
}
}
} while (swapped);
printArray(array);
return amountOfSwaps;
}
// Here we swap values to sort them
void swap(int & value1, int & value2)
{
// We specify a temp and use it to swap
int temp=value1;
value1=value2;
value2=temp;
}
int[] nums = { 12 , 5 , 16 , 9 , 25 , 3 , 45 , 11 , 14 };
int temp;
for(int y = 1 ; y < nums.length - 1 ; y++) {
for(int x = 0 ; x < nums.length - y ; x++) {
if(nums[x] > nums[x+1]) {
temp = nums[x];
nums[x] = nums[x+1];
nums[x+1] = temp;
}
}
}
int[]nums={12,5,16,9,25,3,45,11,14};
内部温度;
对于(int y=1;ynums[x+1]){
温度=nums[x];
nums[x]=nums[x+1];
nums[x+1]=温度;
}
}
}
if(array[i]>array[j+1]){//我们在这里交换函数swap(array[i],array[j+1]);//我们测量交换amountOfSwaps+=1的次数;}
我想你的意思是前两个中的我,但现在它没有对第一个数字进行排序。不,我的意思正是我所说的。试一试。变量我应该只控制内部循环的迭代次数。变量j实际上负责重新排序。好吧,这很有趣,让我试试。现在它在ascen中进行排序按顺序排列我希望它从最高到最小,所以我应该把+1改成右边?我把它改成了if(array[j+1]>array[j])交换(array[j+1],array[j]);
但它仍然没有对最后一个数字排序。。