迭代快速排序在数组中重复第一个数字

时间:2019-04-10 23:28:59

标签: java data-structures

当我尝试用飞机的航班号对飞机阵列进行排序时,我希望它能按顺序显示飞机的优先级。例如:飞机A优先级:1,飞机B优先级:2飞机C优先级:3飞机D优先级:4飞机E优先级:5

但是我的排序一直重复着像飞机A优先级:1飞机B优先级:1 ...等

我尝试只用Airplanes(容纳飞机的数组的名称)替换占位符int,但之前我已经这样做了,但是我缺少什么吗?

// Java program for implementation of QuickSort 
import java.util.*; 

 class IterativeQuickSort 
 { 
/* This function takes last element as pivot, 
places the pivot element at its correct 
position in sorted array, and places all 
smaller (smaller than pivot) to left of 
pivot and all greater elements to right 
of pivot */
static int partition(Airplane[] arr, int low, int high) 
{ 
    Airplane pivot = arr[high]; 
   int i = (low-1); // index of smaller element 
    for (int j=low; j<=high-1; j++) 
    { 
        // If current element is smaller than or 
        // equal to pivot 
        if (arr[j].compareTo(pivot) <= 0) 
        { 
            i++; 

            // swap arr[i] and arr[j] 
            Airplane temp = arr[i]; 
            arr[i] = arr[j]; 
            arr[j] = temp; 
        } 
    } 

    // swap arr[i+1] and arr[high] (or pivot) 
    Airplane temp = arr[i+1]; 
    arr[i+1] = arr[high]; 
    arr[high] = temp; 

    return i+1; 
} 

/* The main function that implements QuickSort() 
arr[] --> Array to be sorted, 
low --> Starting index, 
high --> Ending index */
static void sort(Airplane[] arr, int low, int high) 
{ 
    if (low < high) 
    { 
        /* pi is partitioning index, arr[pi] is 
        now at right place */
        int pi = partition(arr, low, high); 

        // Recursively sort elements before 
        // partition and after partition 
        sort(arr, low, pi-1); 
        sort(arr, pi+1, high); 
    } 
  } 
}

0 个答案:

没有答案