Java 我的泡泡排序赢了';行不通

Java 我的泡泡排序赢了';行不通,java,sorting,bubble-sort,Java,Sorting,Bubble Sort,我有一个编程任务交给我去重新创建排序算法,除了我的冒泡排序,它们似乎都做得很好。我一直在看这个代码,但我似乎无法理解它。我希望你们能看到我的错误。在书中,我被赋予了伪代码: BubbleSort(list,N) //list is the elements to be put in order // N is the number of elements in the list numberOfPairs = N swappedElements = true while(swappedEleme

我有一个编程任务交给我去重新创建排序算法,除了我的冒泡排序,它们似乎都做得很好。我一直在看这个代码,但我似乎无法理解它。我希望你们能看到我的错误。在书中,我被赋予了伪代码:

BubbleSort(list,N)
//list is the elements to be put in order
// N is the number of elements in the list
numberOfPairs = N
swappedElements = true
while(swappedElements) do         
  numberOfPairs = numberOfPairs-1
  swappedElements = false
  for(i = 1 to numberOfPairs) then
    if(list[i] >list[i+1])then
      swap(list[i], list[i+1]
      swappedElements = true
    end if
  end for
end while
好吧,这就是我得到的。然后我创建了这个类:

public class Bubble_Sort {

    private int[] unsorted;
    private int number;
    private int compareCount = 0;
    private int swapCount = 0;
    private boolean swap = true;

    public void sort(int[] unsorted) {
        this.unsorted = unsorted;
        while (swap) {
            swap = false;

            for (int j = 1; j <= unsorted.length - 1; j++) {
                if (unsorted[j - 1] < unsorted[j]) {
                    compareCount++;
                    swap(j - 1, j);

                    swap = true;
                }
            }
        }
    }

    private void swap(int i, int j) {
        swapCount++;
        int num = unsorted[i];
        unsorted[i] = unsorted[j];
        unsorted[j] = num;
    }

}
600℃下的气泡运动试验:

sorted List: [173, 341, 299, 439, 237, 67, 411, 592, 468, 286, 272, 404, 490, 52, 353, 317, 101, 299, 517, 571, 468, 226, 423, 153, 195, 416, 93, 432, 536, 536, 313, 292, 361, 578, 596, 275, 462, 472, 410, 227, 129, 560, 316, 11, 138, 27, 465, 575, 516, 184, 450, 454, 45, 34, 594, 247, 352, 294, 507, 552, 77, 331, 88, 234, 121, 323, 333, 378, 245, 394, 470, 207, 464, 473, 598, 73, 380, 583, 185, 79, 590, 412, 272, 57, 266, 520, 252, 236, 228, 400, 21, 195, 6, 273, 162, 292, 494, 80, 172, 532, 565, 547, 64, 559, 387, 516, 56, 395, 388, 62, 206, 397, 188, 230, 289, 567, 333, 416, 180, 343, 381, 118, 99, 224, 528, 109, 52, 62, 472, 486, 564, 262, 481, 350, 198, 536, 422, 4, 150, 183, 155, 384, 449, 438, 373, 505, 336, 476, 439, 491, 462, 184, 468, 135, 212, 366, 429, 289, 411, 338, 371, 71, 516, 158, 448, 578, 26, 342, 567, 427, 520, 158, 108, 277, 168, 152, 547, 56, 127, 292, 68, 578, 465, 265, 483, 165, 474, 502, 65, 370, 507, 594, 561, 362, 119, 354, 386, 165, 219, 594, 107, 540, 201, 22, 263, 71, 4, 194, 562, 531, 347, 183, 34, 347, 190, 24, 342, 415, 227, 307, 287, 584, 54, 217, 335, 336, 217, 355, 304, 347, 476, 154, 577, 6, 176, 268, 129, 27, 588, 530, 564, 38, 304, 183, 480, 228, 555, 234, 229, 200, 377, 239, 328, 547, 588, 494, 542, 27, 431, 213, 464, 568, 198, 91, 477, 187, 94, 553, 569, 30, 93, 399, 395, 128, 525, 545, 218, 87, 243, 143, 474, 317, 476, 278, 32, 519, 398, 138, 355, 84, 331, 55, 503, 521, 497, 4, 333, 288, 522, 6, 404, 20, 343, 214, 345, 320, 102, 440, 427, 447, 489, 171, 400, 154, 162, 18, 438, 419, 403, 580, 205, 271, 156, 362, 501, 554, 364, 498, 108, 253, 560, 94, 372, 584, 461, 28, 249, 588, 194, 414, 95, 266, 422, 559, 306, 367, 137, 103, 308, 183, 581, 597, 47, 524, 493, 592, 403, 415, 191, 397, 372, 202, 402, 438, 33, 86, 274, 538, 543, 385, 467, 183, 13, 259, 357, 160, 7, 291, 310, 223, 12, 278, 151, 97, 196, 360, 292, 360, 183, 553, 27, 121, 8, 27, 439, 177, 225, 361, 272, 47, 460, 227, 500, 192, 322, 370, 437, 190, 521, 311, 475, 245, 161, 563, 294, 451, 492, 227, 444, 165, 172, 565, 404, 405, 77, 336, 527, 82, 274, 544, 502, 566, 584, 422, 126, 314, 4, 399, 21, 319, 507, 180, 550, 309, 386, 500, 440, 179, 235, 13, 298, 467, 316, 198, 296, 568, 34, 541, 593, 21, 150, 64, 231, 280, 147, 550, 530, 502, 131, 175, 229, 322, 55, 483, 360, 466, 70, 115, 93, 381, 55, 100, 341, 313, 208, 494, 39, 535, 89, 108, 194, 175, 371, 574, 145, 553, 482, 296, 103, 392, 53, 482, 29, 70, 421, 60, 174, 467, 431, 511, 209, 122, 337, 167, 213, 317, 444, 294, 197, 12, 118, 543, 31, 2, 262, 241, 503, 483, 224, 426, 30, 256, 348, 528, 284, 278, 321, 171, 187, 304, 153, 328, 526, 364, 402, 550, 484, 382, 555, 83, 577, 200, 530, 337, 4, 348, 563, 182, 40, 17, 258, 319, 445, 301, 513, 373, 138, 404, 159, 413, 158, 298, 171, 157, 41, 252, 226, 143, 373, 584, 68, 589, 595, 271, 277, 262, 425, 318, 184, 250, 425, 206, 422, 449, 324, 123, 410, 504, 159, 288]
然后我运行了不同大小的数组,由于@Mihnea DB,我原来的问题得到了解决,但现在它不会通过第一次迭代

  Bubble_Sort reBubble = new Bubble_Sort();
  int[] bubbleArray;    
 for(int N = 100; N<1100; N+=100)
      {
     bubbleArray = new int[N];
        for(int i = 0; i<N; i++)
        {           
        bubbleArray[i] = (int)(Math.random()*N);            
        }
        System.out.println("BUBBLESORT TEST at " + N);
        reBubble.sort(bubbleArray);
        System.out.println("sorted List: "+ Arrays.toString(bubbleArray));
         }
Bubble_Sort reBubble=new Bubble_Sort();
int[]bubbleArray;
对于(int N=100;N
public void排序(int[]未排序)
{       
for(int i=0;i
if(unsorted[j-1]<unsorted[j]) 

因为您使用的是比(
您可能想用一个更短的数组来测试它。@ChthonicProject是的,仍然不起作用。使用一个短数组,您可以更轻松地逐步调试它,看看哪里出了问题。凯文,我已经恢复了您原来问题的错误。请检查该问题是否仍然有效,并接受其中一个答案下面的s指出了错误。请注意,您不必为每个步骤迭代整个数组;最大值将冒泡到顶部(因此该算法的名称),所以在每次循环到N之后,你可以将N减少1。for循环是可以的,但我真的尝试按照我的书使用while循环。这肯定有帮助,但大于100的数组仍然不够。100个元素的数组可以排序,但任何大于200的元素都不行。我用你给出的输入进行了测试,数组是sorted激怒了它,但对于大于100的数组,它不会排序这真的很有趣。我正在通过一个循环来增加数组的大小。它在第一次运行时起作用,但在其余时间不起作用。我将编辑我的帖子来添加我所说的内容。可能我误解了你,但你不能在多个数组上重复使用该函数。你知道吗在通话之间,必须重新初始化
交换
真实
。啊,是的,成功了。太棒了!非常感谢您的帮助!
public void sort(int[] unsorted)
{       
    for(int i=0; i < unsorted.length, i++) {
        for(int j=0; j<unsorted.length-1; j++)
        {
            if(unsorted[j+1]<unsorted[j]) {
                swap(j-1,j);
            }
        }
     }
}
if(unsorted[j-1]<unsorted[j]) 
if(unsorted[j-1] > unsorted[j])
[3, 2, 4, 5, 1, 0, 2]
[3, 4, 5, 2, 1, 0, 2]
[4, 5, 3, 2, 1, 0, 2]
[5, 4, 3, 2, 1, 0, 2]
[5, 4, 3, 2, 1, 0, 2]