我的冒泡排序不起作用

时间:2014-02-16 21:04:21

标签: java sorting bubble-sort

我有一个编程任务给我重新创建排序算法,好吧他们似乎都做得很好,除了我的冒泡排序。我一直在看这个代码,我似乎无法弄明白。我希望你们能看到我出错的地方。在书中我给了psuedocode:

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;
    }

}

这是从0到600的一些随机数组输出的内容:

Unsorted List: [217, 207, 169, 124, 409, 304, 370, 232, 179, 409, 447, 134, 579, 228, 216, 452, 171, 55, 544, 518, 197, 10, 44, 416, 394, 161, 573, 399, 5, 394, 424, 303, 368, 508, 9, 178, 372, 296, 289, 391, 255, 386, 50, 491, 450, 329, 114, 405, 113, 512, 175, 206, 341, 586, 255, 216, 539, 258, 325, 94, 520, 412, 278, 550, 508, 147, 517, 416, 73, 275, 431, 77, 350, 457, 5, 551, 292, 573, 582, 421, 575, 178, 108, 598, 493, 171, 484, 160, 597, 73, 516, 200, 95, 30, 577, 475, 360, 586, 86, 198, 128, 598, 305, 203, 208, 442, 49, 280, 502, 433, 48, 254, 526, 185, 540, 324, 319, 47, 346, 251, 374, 10, 65, 105, 240, 105, 19, 449, 538, 13, 521, 562, 100, 93, 361, 500, 341, 333, 85, 398, 98, 92, 504, 5, 521, 7, 281, 509, 224, 54, 444, 537, 26, 102, 250, 105, 566, 126, 109, 566, 521, 58, 29, 335, 467, 320, 470, 70, 182, 300, 557, 169, 66, 379, 181, 567, 443, 347, 321, 50, 85, 223, 30, 251, 390, 390, 66, 173, 112, 336, 9, 265, 373, 57, 165, 197, 533, 376, 368, 494, 596, 235, 586, 154, 266, 466, 171, 15, 400, 174, 352, 58, 548, 156, 217, 84, 475, 244, 128, 80, 22, 400, 535, 491, 210, 461, 321, 284, 93, 251, 234, 80, 540, 56, 445, 303, 84, 515, 403, 345, 575, 515, 231, 38, 263, 358, 137, 411, 486, 245, 127, 269, 373, 23, 508, 14, 427, 404, 173, 355, 49, 137, 233, 268, 467, 160, 593, 7, 432, 268, 563, 566, 17, 588, 398, 227, 474, 122, 457, 279, 215, 2, 36, 479, 152, 323, 453, 294, 264, 214, 471, 97, 175, 484, 200, 188, 489, 218, 255, 96, 502, 196, 576, 145, 389, 96, 49, 479, 161, 355, 440, 124, 569, 336, 262, 314, 381, 545, 445, 299, 131, 474, 383, 187, 28, 404, 76, 358, 481, 528, 351, 181, 68, 389, 23, 191, 261, 457, 247, 496, 329, 566, 554, 460, 458, 201, 156, 568, 197, 335, 515, 470, 474, 38, 386, 502, 18, 432, 497, 542, 343, 519, 575, 357, 136, 203, 114, 5, 364, 201, 593, 218, 45, 192, 434, 102, 423, 360, 266, 499, 279, 358, 589, 236, 200, 195, 510, 278, 219, 31, 527, 151, 407, 396, 228, 46, 150, 452, 421, 363, 575, 102, 247, 357, 430, 86, 596, 544, 427, 11, 542, 45, 421, 65, 596, 482, 47, 576, 533, 220, 534, 50, 347, 597, 195, 544, 398, 0, 139, 355, 542, 362, 70, 68, 288, 78, 399, 542, 96, 295, 179, 164, 490, 404, 266, 415, 113, 165, 105, 26, 319, 566, 412, 527, 492, 599, 329, 477, 288, 242, 27, 31, 130, 410, 20, 380, 239, 239, 350, 85, 343, 146, 226, 564, 81, 553, 177, 146, 563, 280, 483, 8, 315, 488, 509, 451, 36, 34, 386, 342, 299, 570, 275, 137, 267, 568, 432, 78, 132, 172, 1, 394, 59, 210, 181, 368, 204, 519, 468, 588, 367, 175, 338, 375, 392, 63, 306, 65, 327, 343, 511, 163, 458, 597, 291, 472, 187, 239, 151, 302, 325, 21, 587, 590, 593, 94, 108, 171, 269, 5, 178, 439, 440, 19, 37, 74, 36, 574, 71, 326, 477, 281, 173, 448, 201, 557, 580, 512, 151, 412, 599, 576, 270, 225, 589, 67, 579, 431, 256, 581, 168, 158, 202, 281, 244, 419, 484, 417, 496, 173, 576, 109, 198, 227, 70, 537, 99, 445, 245, 110, 269, 51, 79, 50, 431, 554, 34, 270, 351, 354]

600岁的BUBBLESORT TEST:

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数据库我的原始问题已得到解决但现在它不会排序通过第一次迭代。

  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));
         }

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

由于您使用小于<)比较运算符,因此您希望将数组从末尾开始

我建议您在此阵列上逐步运行代码:

[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]

正如您所看到的,最后一个2是错误的,因为您的断言(a[i-1] < a[i])仍然存在。

稍后编辑:如果您打算重复使用该类的实例,则应在排序函数中执行swap = true的初始化。

答案 1 :(得分:0)

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);
            }
        }
     }
}

答案 2 :(得分:0)

改变这个:

if(unsorted[j-1]<unsorted[j]) 

if(unsorted[j-1] > unsorted[j])
相关问题