快排 太过爱你忘了你带给我的痛 2024-04-17 16:18 15阅读 0赞 import java.util.Arrays; public class kuaiPai { public static void main(String[] args) { int[] arr = new int[]{5, 3, 4, 1, 6, 322, 66, 2, 78}; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr, int start, int end) { //当开始位置小于结束位置时(数组有数据) 进行排序 也就是递归入口 if (start < end) { //首先找到基准数 作为比较的标准数 取数组开始位置 从哪里开始 用哪个数当标准数 这个就是标准数 int stard = arr[start]; //记录需要进行排序的下标 int low = start; int high = end; //循环比对比标准数大和小的数字 while (low < high) { //如果标准数小于右边的数字 把右边的游标卡尺向左移动 while (low < high && stard <= arr[high]) { high--; } //如果标准数大于 右边的数字 //用低位数字替换右边数字 arr[low] = arr[high]; //如果左边的数字比标准数小 while (low < high && arr[low] <= stard) { low++; } //如果左边的数字比标准数大 //用左边数字替换右边数字 arr[high] = arr[low]; } //把标准数赋给高或者低所在的元素 arr[low] = stard; //处理所有比标准数小的数字 quickSort(arr, start, low); //处理所有比标准数大的数字 quickSort(arr, low + 1, end); } } } package sort; import java.util.*; public class Finder { public static void main(String[] args) { Finder f = new Finder(); int[] array = { 5, 7, 9, 2, 4, 6, 8, 10, 12, 11, 14, 15, 13 }; int re = f.findKthCore(array, 4, 0, array.length - 1); System.out.println(re); } public int findKthCore(int[] a, int K, int left, int right) { //if (left < right) { int old_left = left; int old_right = right; int temp = a[left]; while (left < right) { while (left < right && a[right] > temp) { right--; } a[left] = a[right]; //注意:此处不能使用a[left++] = a[right];,因为当数组为1,3,5,4时(即第一个元素的位置是正确的时候,会出现问题) while (left < right && a[left] < temp) { left++; } a[right] = a[left]; } a[left] = temp; if (left == K - 1) { return a[left]; } else if (left > K - 1) { return findKthCore(a, K, old_left, left - 1); } else { return findKthCore(a, K, left + 1, old_right); } //}else // return a[left]; } }
相关 【模板】——快排 快排 void qSort(int a[], int l, int r) { if (l < r) { int i 淡淡的烟草味﹌/ 2022年10月16日 08:49/ 0 赞/ 92 阅读
相关 快排.....XXX class Solution { public int[] sortArray(int[] nums) { 冷不防/ 2022年09月16日 03:51/ 0 赞/ 180 阅读
相关 快排 \include<stdio.h> int a\[1000\]; void quicksort(int first, int last)\{ i 阳光穿透心脏的1/2处/ 2022年06月13日 00:40/ 0 赞/ 177 阅读
相关 Java快排 package basic_class_01; import java.util.Arrays; public class Code 末蓝、/ 2022年05月19日 09:30/ 0 赞/ 195 阅读
相关 python 快排 快排:讲序列分成左右两部分,在每个部分中选一个基准,比基准小的放到它前面,比基准大的放到它后面。 左右两个指针,同时向中间移动,i++,j–, 实现思路: 分成两部分需 深碍√TFBOYSˉ_/ 2022年05月08日 02:42/ 0 赞/ 205 阅读
相关 快排算法 include <bits/stdc++.h> using namespace std; void swap(int i, int 我就是我/ 2022年03月19日 04:52/ 0 赞/ 232 阅读
相关 快排 public class Tester { public static int getIndex(int[] arr,int low,int 叁歲伎倆/ 2022年03月10日 09:18/ 0 赞/ 218 阅读
相关 无敌快排 快速排序 既然人家名称就叫快速排序,那么自然排序速度快,但是快也是相对而言的,不过大家知道就行了,这是哲学问题,万事万物都是相对的,原谅我也不咋的懂哲学,换一种不同的场景 谁践踏了优雅/ 2022年02月15日 03:28/ 0 赞/ 266 阅读
相关 golang 快排 package main import "fmt" / 快速排序 快速排序 复杂度 N (logN) 待我称王封你为后i/ 2022年01月12日 07:09/ 0 赞/ 241 阅读
相关 python 快排 def quick(lst, start, end): """ 快速排序 复杂度 N (logN) 原理为有两个指针: 矫情吗;*/ 2022年01月12日 07:07/ 0 赞/ 247 阅读
还没有评论,来说两句吧...