导航:首页 > 编程语言 > 快排java

快排java

发布时间:2021-12-08 12:48:27

❶ 快速排序java实现

class Solution {
private void swap(int[] nums, int i, int j) {
if (i != j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

private int partition(int[] nums, int left, int right) {
int i = left - 1;
int pivot = nums[right];
for (int j = left; j < right; ++j) {
if (nums[j] < pivot) {
swap(nums, ++i, j);
}
}
swap(nums, ++i, right);
return i;
}

private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int middle = partition(nums, left, right);
quickSort(nums, left, middle - 1);
quickSort(nums, middle + 1, right);
}

public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
}

❷ 用JAVA实现快速排序算法

本人特地给你编的代码
亲测

public class QuickSort {

public static int Partition(int a[],int p,int r){
int x=a[r-1];
int i=p-1;
int temp;
for(int j=p;j<=r-1;j++){
if(a[j-1]<=x){
// swap(a[j-1],a[i-1]);
i++;
temp=a[j-1];
a[j-1]=a[i-1];
a[i-1]=temp;

}
}
//swap(a[r-1,a[i+1-1]);
temp=a[r-1];
a[r-1]=a[i+1-1];
a[i+1-1]=temp;

return i+1;

}

public static void QuickSort(int a[],int p,int r){

if(p<r){
int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);

}

}

public static void main(String[] stra){

int a[]={23,53,77,36,84,76,93,13,45,23};
QuickSort(a,1,10);

for (int i=1;i<=10;i++)
System.out.println(a[i-1]);

}

}

❸ java实现快速排序

class Solution {
private void swap(int[] nums, int i, int j) {
if (i != j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

private int partition(int[] nums, int left, int right) {
int i = left - 1;
int pivot = nums[right];
for (int j = left; j < right; ++j) {
if (nums[j] < pivot) {
swap(nums, ++i, j);
}
}
swap(nums, ++i, right);
return i;
}

private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int middle = partition(nums, left, right);
quickSort(nums, left, middle - 1);
quickSort(nums, middle + 1, right);
}

public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
}

❹ 怎么用java实现快速排序

package com.xinhua.test2;
public class Student {
int sno;
String name;
double chiness;
double math;
double english;
double three_sco;
double all_s;
public Student(int sno, String name, double chiness, double math, double english, double three_sco) {
this.sno = sno;
this.name = name;
this.chiness = chiness;
this.math = math;
this.english = english;
this.three_sco = three_sco;
}
@Override
public String toString() {
return "学号:" + sno + ", 名字:" + name + ", 语文成绩:" + chiness + ", 数学成绩:" + math + ", 英语成绩:"
+ english + "总成绩:"+(chiness+math+english+three_sco);
}
public static void main(String[] args) {
Student A=new Student(1, "张三", 118, 145, 114.5, 198);
Student B=new Student(2, "李四", 130,110.5,100,210);
Student C=new Student(3, "王五",142.5,120,87.5,245.5);
System.out.println("学生列表信息为:");
System.out.println(A.toString());
System.out.println(B.toString());
System.out.println(C.toString());
//
double a_scoAll=A.chiness+A.math+A.english+A.three_sco;
A.all_s=a_scoAll;
double b_scoAll=B.chiness+B.math+B.english+B.three_sco;
B.all_s=b_scoAll;
double c_sclAll=C.chiness+C.math+C.english+C.three_sco;
C.all_s=c_sclAll;
Student[] s_s={A,B,C};
System.out.println("按总成绩从大到小排序为");
Student temp;
for(int i=0;i<s_s.length;i++){
for(int j=1;j<s_s.length-i;j++){
if(s_s[j-1].all_s < s_s[j].all_s){
temp=s_s[j-1] ;
s_s[j-1] =s_s[j];
s_s[j]=temp;
}
}
}
for(int i=0;i<s_s.length;i++){
System.out.println("第"+(i+1)+"名为:"+s_s[i].toString());
}
}
}

❺ java中快速排序的算法举个例子

package person.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
* class name: RapidSort
* description: Java快速排序法:数组和集合
* @author Jr
*
*/
public class RapidSort {
private Random ran = new Random(); // 声明一个全局变量ran,用来随机生成整数

/**
* method name: sortArray
* description: 对数组的快速排序,只能用于int[]类型的数组
* @return
*/
private void sortArray() {
int[] array = new int[10]; // 声明数组长度为10
for (int i = 0 ; i < array.length; i++) {
array[i] = ran.nextInt(10) + 1; // 数组赋值
}
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}

/**
* method name: sortList
* description: 对集合的快速排序,可以用于List<Object>类型数组,
* 隐含意思就是对所有类型数组都适用
* @return
*/
private void sortList() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0 ; i < 10; i++) {
list.add(ran.nextInt(10) + 1); // 给集合赋10个值
}
Collections.sort(list);
System.out.println(list);
}

public static void main(String[] args) {
RapidSort rs = new RapidSort();
rs.sortArray();
rs.sortList();
}
}

❻ 快速排序 java版的 谢谢了!

你的不是不能运行.. 而是有死循环
sort()方法中的语句
while (i<j && arr[i]<povit );
此while循环没有改变循环步长的代码,所以执行到此时,程序会进入死循环,
根据你代码的大致思路,把这个";"去掉就可以了
(还有你下面的while循环也要去掉第一个";" while循环是不用加";"的 与do while循环不同,注意语法)

❼ java快速排序

public class demo11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
QuickSort quick = new QuickSort();
int arr[] = { 4, 2, 6, 1, 5, 0, 8, -1 };
quick.Sort(arr, 0, arr.length-1);
for(int i:arr)
System.out.println(i);
}
}

class QuickSort {
public void Sort(int arr[], int left, int right) {
if(left == right) return;
System.out.println("sort:");
for(int i=left;i<=right;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
int inser = arr[left];
int temp;
int le = left;
int re = right ;
while (le < re) {
while (le < re && inser < arr[re]) {
re--;
}
if(re==le) break;
temp = arr[re];
arr[re] = arr[le];
arr[le] = temp;
le++;
while (le < re && inser > arr[le]) {
le++;
}
if(re==le) break;
temp = arr[re];
arr[re] = arr[le];
arr[le] = temp;
re--;
}
arr[le]=inser;
if(le>left)
Sort(arr, left, le-1);
if(re<right)
Sort(arr,le+1,right);
}
}

排序的思路是:取数组的第一个数(arr[left])为参考值(inser),将比参考值(inser)小的数全部放到参考值左边,比参考值(inser)大的全部放到参考值右边。然后用相同的方法对参考值右边和左边的数组进行排序。

❽ 如何用java实现快速排序,简答讲解下原理

快速排序思想:
通过对数据元素集合Rn 进行一趟排序划分出独立的两个部分。其中一个部分的关键字比另一部分的关键字小。然后再分别对两个部分的关键字进行一趟排序,直到独立的元素只有一个,此时整个元素集合有序。
快速排序的过程,对一个元素集合R[ low ... high ] ,首先取一个数(一般是R[low] )做参照 , 以R[low]为基准重新排列所有的元素。
所有比R[low]小的放前面,所有比R[low] 大的放后面,然后以R[low]为分界,对R[low ... high] 划分为两个子集和,再做划分。直到low >= high 。
比如:对R={37, 40, 38, 42, 461, 5, 7, 9, 12}进行一趟快速排序的过程如下(注:下面描述的内容中元素下表从 0 开始):
开始选取基准 base = 37,初始位置下表 low = 0 , high = 8 , 从high=8,开始如果R[8] < base , 将high位置中的内容写入到R[low]中, 将high位置空出来, low = low +1 ;
从low开始探测,由于low=1 , R[low] > base ,所以将R[low]写入到R[high] , high = high -1 ;
检测到low < high ,所以第一趟快速排序仍需继续:
此时low=1,high=7,因为 R[high] < base ,所以将 R[high] 写入到到R[low]中,low = low + 1;
从low开始探测,low = 2 , R[low] >base ,所以讲R[low]写入到R[high],high=high-1;
继续检测到 low 小于high
此时low=2,high=6,同理R[high] < base ,将R[high] 写入到R[low]中,low=low+1;
从low继续探测,low = 3 , high=6 , R[low] > base , 将R[low]写入到R[high]中,high = high-1;
继续探测到low小于high
此时low=3,high=5,同理R[high] < base,将R[high]写入到R[low]中,low = low +1;
从low继续探测,low = 4,high=5,由于R[low] > base , 将R[low]写入到R[high]中,high = high -1 ;
此时探测到low == high == 4 ;该位置即是base所在的位置,将base写入到该位置中.
然后再对子序列Rs1 = {12,9,7,5} 和 Rs2={461,42,38,40}做一趟快速排序,直到Rsi中只有一个元素,或没有元素。
快速排序的Java实现:
private static boolean isEmpty(int[] n) {

return n == null || n.length == 0;
}

// ///////////////////////////////////////////////////
/**
* 快速排序算法思想——挖坑填数方法:
*
* @param n 待排序的数组
*/
public static void quickSort(int[] n) {
if (isEmpty(n))
return;
quickSort(n, 0, n.length - 1);
}

public static void quickSort(int[] n, int l, int h) {
if (isEmpty(n))
return;
if (l < h) {
int pivot = partion(n, l, h);
quickSort(n, l, pivot - 1);
quickSort(n, pivot + 1, h);
}
}

private static int partion(int[] n, int start, int end) {
int tmp = n[start];
while (start < end) {
while (n[end] >= tmp && start < end)
end--;
if (start < end) {
n[start++] = n[end];
}
while (n[start] < tmp && start < end)
start++;
if (start < end) {
n[end--] = n[start];
}
}
n[start] = tmp;
return start;
}
在代码中有这样一个函数:
public static void quickSortSwap(int[] n, int l, int h)
该函数可以实现,元素集合中特定的 l 到 h 位置间的数据元素进行排序。

❾ 求使用java实现的快排算法

① 代码:

publicclassquicksortdemo{

privateintarray[];
privateintlength;

publicvoidsort(int[]inputArr){

if(inputArr==null||inputArr.length==0){
return;
}
this.array=inputArr;
length=inputArr.length;
quickSort(0,length-1);
}

privatevoidquickSort(intlowerIndex,inthigherIndex){

inti=lowerIndex;
intj=higherIndex;
//calculatepivotnumber
intpivot=array[lowerIndex+(higherIndex-lowerIndex)/2];
//Divideintotwoarrays
while(i<=j){
while(array[i]<pivot){
i++;
}
while(array[j]>pivot){
j--;
}
if(i<=j){
swap(i,j);
i++;
j--;
}
}
//callquickSort()methodrecursively
if(lowerIndex<j)
quickSort(lowerIndex,j);
if(i<higherIndex)
quickSort(i,higherIndex);
}

privatevoidswap(inti,intj){
inttemp=array[i];
array[i]=array[j];
array[j]=temp;
}

publicstaticvoidmain(Stringa[]){

quicksortdemosorter=newquicksortdemo();
int[]input={24,2,45,20,56,75,2,56,99,53,12};
sorter.sort(input);
for(inti:input){
System.out.print(i);
System.out.print("");
}
}
}

② 运行:

c:>javaquicksortdemo
22122024455356567599

❿ Java快速排序

原理:
快速排序也是分治法思想的一种实现,他的思路是使数组中的每个元素与基准值(Pivot,通常是数组的首个值,A[0])比较,数组中比基准值小的放在基准值的左边,形成左部;大的放在右边,形成右部;接下来将左部和右部分别递归地执行上面的过程:选基准值,小的放在左边,大的放在右边。。。直到排序结束。
步骤:
1.找基准值,设Pivot = a[0]
2.分区(Partition):比基准值小的放左边,大的放右边,基准值(Pivot)放左部与右部的之间。
3.进行左部(a[0] - a[pivot-1])的递归,以及右部(a[pivot+1] - a[n-1])的递归,重复上述步骤

阅读全文

与快排java相关的资料

热点内容
教育大数据应用典型示范项目 浏览:934
有什么好的消化内科科普网站 浏览:504
打开网络映射存储为什么特别慢 浏览:157
苹果手机摔弯了还能修吗 浏览:532
华中数控编程怎么换 浏览:874
word2007画图工具在哪里 浏览:565
广播式网络由什么组成 浏览:538
福州老用户升级4g校园套餐 浏览:644
jstr加点击事件 浏览:652
搜狗输入法最老版本下载地址 浏览:145
玛卡编程怎么样 浏览:302
怎么编程伺服器编码 浏览:109
什么是机密文件 浏览:258
网站收录量低应该如何解决 浏览:978
飞跃贷app官网 浏览:337
js正则表达式全为整数 浏览:561
在哪里免费下载大数据 浏览:218
linux怎么做视频网站 浏览:949
安卓舰娘登入不进去 浏览:145
ak47龙鳞升级成什么 浏览:256

友情链接