java——快排、冒泡、希尔、归并
直接贴代码
快排:
public class Test {
private static void sort(int[] nums){
if(nums == null || nums.length == 0){
return;
}
quickSort(nums, 0, nums.length-1);
}
private static void quickSort(int[] nums, int start, int end) {
int low = start;
int high = end;
int temp = nums[start];
while(start<end){
while (start<end && nums[end]>=temp){
end--;
}
swap(nums, start, end);
while(start<end && nums[start]<=temp){
start++;
}
swap(nums, start, end);
}
if(start-1>low) {
quickSort(nums, low, start - 1);
}
if(high>start+1) {
quickSort(nums, start + 1, high);
}
}
private static void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void main(String [] args){
int[] nums = {49,22,34, 6,22,19,3,19};
sort(nums);
for(int i=0;i<nums.length;i++){
System.out.print(nums[i]+“ ”);
}
}
}
冒泡:
private static void bubbleSort(int[] num){
for(int i = 0 ; i < num.length ; i ++) {
for(int j = 0 ; j < num.length - i - 1 ; j ++){
if(num[j]>num[j+1]){
swap(num, j , j+1);
}
}
}
}
改进冒泡:
public void bubbleSort(int arr[]) {
boolean didSwap;
for(int i = 0, len = arr.length; i < len - 1; i++) {
didSwap = false;
for(int j = 0; j < len - i - 1; j++) {
if(arr[j + 1] < arr[j]) {
swap(arr, j, j + 1);
didSwap = true;
}
}
if(didSwap == false)
return;
}
}
改进后的冒泡排序最佳情况下时间复杂度为O(n)
希尔排序:
private static void shellSort(int[] nums){
for(int d = nums.length/2 ; d > 0 ; d = d / 2){
for(int i = d ; i < nums.length ; i ++){
for(int j = i; j>=d; j = j-d){
if (nums[j] < nums[j - d]) {
swap(nums, j - d, j);
}else{
break;
}
}
}
}
}
归并排序:
private static void mergeSort(int[] nums, int low, int high){
if(low>=high){
return;
}
int mid = (low+high)/2;
mergeSort(nums, low, mid);
mergeSort(nums, mid + 1, high);
merge(nums,low, mid, high);
}
private static void merge(int[] nums, int low, int mid, int high){
int[] temp = new int[high-low+1];
int i = low;
int j = mid+1;
int k = 0;
while(i<=mid&&j<=high){
if(nums[i]<nums[j]){
temp[k++] = nums[i++];
}else{
temp[k++] = nums[j++];
}
}
while(i<=mid){
temp[k++] = nums[i++];
}
while(j<=high){
temp[k++] = nums[j++];
}
for(int p = 0 ; p < temp.length ; p++){
nums[low+p] = temp[p];
}
}
java——快排、冒泡、希尔、归并的更多相关文章
- Java 快排
基于分治法的快排,用递归实现. 首先讲一下实现的过程. 1.在数组中取一个数作为基准,所谓的基准就是用来对比的数. 2.然后在数组中从后往前找,找到一个逆序数为止,找到之后就把它的值赋值到基准数的位, ...
- Java 快排 排序
一.快排的一种 ==================== public class myMain { public static void main(String[] args) { int t[] ...
- java快排(两种方法)
快排是最基础的排序算法之一,今天来回顾一下. public class QuickSort { public static void quickSort(int[] array){ if(array ...
- [排序] 快排 && 冒泡(自己写)
#include <iostream> using namespace std; /* 快速排序 通过一趟排序,以轴点为界 分割为两部分:左部分 <= 轴点 <= 右部分 再分 ...
- JS算法之快排&冒泡
1.快速排序思想: 1.1 先找数组的最中间的一个数为基准 1.2 把数组通过此基准分为小于基准的left数组和大于基准的right数组, 1.3 递归重复上面的两个步骤, 代码如下: functio ...
- java快排思想
1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }
- 使用Python完成排序(快排法、归并法)
class Sort(object): def quick_sort(self, ls): self.quick_sort_helper(ls, 0, len(ls) - 1) return ls d ...
- Java快排
package quickSort; /** * 快速排序 * @author root * */ public class QuickSort { static int[] data = {0,2, ...
- Java 排序(快排,归并)
Java 排序有Java.util.Arrays的sort方法,具体查看JDK API(一般都是用快排实现的,有的是用归并) package yxy; import java.util.Arrays; ...
随机推荐
- Condition实现多个生产者多个消费者
Condition实现多对多交替打印: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.R ...
- 在PyCharm 软件中设置你的项目 使用的Python版本
在PyCharm 软件中设置你的项目 使用的Python版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 就运行不了:使用python3编 ...
- spoj2142 Arranging Flowers
传送门 题目大意 给你n和m,表示一个n*n的数独已经填完了m行,让你填出剩下几行,要求答案的字典序最小. 分析 看到这道题我首先想到的是记录每行每列使用了哪些数字,然后贪心的来填,但是发现用这种策略 ...
- Luogu 1514 [NOIP2010] 引水入城
我就是过来开心一下……这道题从开坑以来已经堆积了大半年了……今天才发现广搜一直写挂…… 丢个线段覆盖的模板,设$f_{i}$表示覆盖区间[1, i]的最小代价,$g_{i, j}$表示覆盖区间[i, ...
- boost库thread.hpp编译警告honored已修复
请浏览:https://svn.boost.org/trac/boost/ticket/7874 #7874: compile warning: thread.hpp:342: warning: ty ...
- Java的post请求-----接口测试
本次主要是对登陆的接口测试post请求,希望记录在博客里面,一点一点的成长. package com.ju.Login; import java.io.BufferedReader; import j ...
- ubuntu - 14.04,如何操作Gnome的任务栏?
搜索到的答案: in gnome classic you must press both the Alt & Super keys at the same time while right-c ...
- UIColor
UIColor.CIColor 和 CGColor 出现在不同的类库里面,其实就是颜色存储方式不同而已,比如 999 可以用 10 进制.2 进制.16 进制等存储.三者之间都是能够方便转换的,特别是 ...
- 「BZOJ1010」[HNOI2008] 玩具装箱toy(斜率优化)
P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为 1⋯N1\cdots N1⋯N ...
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
原文链接:https://blog.csdn.net/hq091117/article/details/79065199 https://blog.csdn.net/allen_tsang/artic ...