您的位置:首页 > 其它

【快速】排序, T(n) = O(nlgn),O(n^2) S(n) = O(lgn) --- 不稳定

2013-12-14 20:03 411 查看
  public static void quickSort(RS[] arrRS,int low,int high){

// 保存【分割点】
int partition;

// 【段长】大于1
if(low < high){

// 一次分割,获得【分割点】 和  两个【子段】
partition = quickPartition(arrRS,low,high);

//【递归】两个子段
quickSort(arrRS, low,           partition-1);
quickSort(arrRS, partition+1, high);
}
  }

  //分割
19   private static int quickPartition(RS[] arrRS, int left, int right) {

// 拿起左边第一个,作【标尺】
RS tempRS = arrRS[left];

// 【段长】大于1,未结束
while (left < right) {

/*################  左     <<===   右  ##################*/
while (left < right && arrRS[right].val>=tempRS.val) {
right--;
}
if (left < right) {  // 若未结束,右边大的,放到左边 left 索引处,left 右移
arrRS[left] = arrRS[right];
left ++;
}

/*################  左      ===>>   右  ##################*/
while (left < right && arrRS[left].val<tempRS.val) {
left++;
}
if (left < right) {  // 若未结束,左边小的,放到右边 right 索引处,right 左移
arrRS[right] = arrRS[left];
right --;
}

}

// 【标尺】归【分割点】
arrRS[left] = tempRS;

// 返回分割点
return left;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: