1/排序算法:冒泡排序bubble sort,插入排序 insertion sort,选择排序 selection sort,快速排序 quick sort,归并排序 merge sort;堆排序 heap sort---基于排序

桶排序bucket sort  一种特殊情况下的排序。

2/实现

1)冒泡排序bubble sort:从位置0开始,一次比较到length - 0,前面大交换;再从位置1开始,依次类推。

 public void bubbleSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = 1; j < nums.length - i; j++) {
if (num[j - 1] > nums[j]) {
swap(nums, j - 1, j);
}
}
}
} public void swap(int[] nums, int i , int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

冒泡排序有一个优化算法,就是在一个排好序的数组,一轮比较没有swap,则是排好序的,那么直接返回

  1 public void bubbleSort(int[] nums) {
2 for (int i = 0; i < nums.length; i++) {
boolean isSwap = false;
3 for (int j = 1; j < nums.length - i; j++) {
4 if (num[j - 1] > nums[j]) {
5 swap(nums, j - 1, j);
isSwap = true;
6 }
7 }
if (!isSwap) {
return;
}
8 }
9 }
10
11 public void swap(int[] nums, int i , int j) {
12 int temp = nums[i];
13 nums[i] = nums[j];
14 nums[j] = temp;
15 }

2)插入排序insertion sort:先把前i个数字排好序,然后再把前i+1个数字排好序。

 public void insertSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
for (int j = i; j > 0; j --) {
if (nums[j - 1] > nums[j ]) {
swap(nums, j - 1; j);
}
}
}
} public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

3)选择排序selection sort:先从N里面选出最小的一个值,与0位置数字交换,再从剩下数字里面选出最小的值跟1位置交换。依次类推。

 public void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {//i < nums.length -1,j要从i + 1开始
int min = nums[i];
int minIndex = i;
for (int j = i + 1; j > nums.length; j++) {
if (num[j] < min) {
min = num[j];
minIndex = j;
}
}
num[minIndex] = num[i];
nums[i] = min;
}
}

4)快速排序quick sort:

 public void quickSort(int[] nums) {
sort(nums, 0, nums.length);
} publc void sort(int[] nums, int begin, int end) {
if (begin >= end) {
return;
}
int pivotIndex = partition(nums, begin, end);
sort(nums, begin, pivotIndex - 1);//pivot已经就位,所以跳过
sort(nums, pivotIndex + 1, end);
} public partition(int[] nums, int begin, int end) {
int pivot = num[begin];
while (begin < end) {
while (begin < end && num[end] > pivot) {
end--;
}
nums[begin] = nums[end];
while (begin < end && nums[begind] <= pivot) {
begin++;
}
nums[end] = nums[begin];
}
nums[begin] = pivot;
return begin;
}

Lecture--9 Sorting的更多相关文章

  1. Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了

    作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...

  2. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  5. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  6. note of introduction of Algorithms(Lecture 3 - Part1)

    Lecture 3(part 1) Divide and conquer 1. the general paradim of algrithm as bellow: 1. divide the pro ...

  7. U3D sorting layer, sort order, order in layer, layer深入辨析

    1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...

  8. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

  9. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

  10. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

随机推荐

  1. Python知识点:distutils常用子模块

    from distutils.core import setup, Extension, Commandfrom distutils.command.build import buildfrom di ...

  2. VisualGDB系列5:使用VS来开发Linux程序

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文演示如何使用VS来构建和调试Linu ...

  3. 问题:C# Dictionary嵌套使用;结果:嵌套Dictionary添加 , C#2.0泛型详细介绍

    Dictionary<int, Dictionary<string, string>> dict1 = new Dictionary<int, Dictionary< ...

  4. linux命令-vim编辑模式

    按  i  键 进去编辑模式 左下角显示 插入 按 I  键 进入编辑模式 光标到行首 按 a 键 在光标的后一位 按A 键 光标在行尾 按 o 键 在光标下面另起一行 按O 键  在光标上面另起一行 ...

  5. viewstate的基本用法

    转自:http://www.cnblogs.com/ooip/p/4743536.html 在web窗体将控件属性设置为runat=server时,这个控件会被添加一个隐藏属性_ViewState,_ ...

  6. 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree

    ###https://www.biostars.org/p/195758/ Left reads: Input : 49801387 Mapped : 46258301 (92.9% of input ...

  7. MyBatis入门基础

    转自http://www.cnblogs.com/selene/p/4604605.html 话不多说,先看看原始的JDBC程序代码,看看这样的代码存在什么样子的问题. package com.uti ...

  8. 6.wireshark使用全解

    开始之前先推荐一个wireshark视频: http://www.shiyanbar.com/course-video/watchVideo/cid/419/vid/2001 页面功能简介 1.主界面 ...

  9. NMF非负矩阵分解

    著名的科学杂志<Nature>于1999年刊登了两位科学家D.D.Lee和H.S.Seung对数学中非负矩阵研究的突出成果.该文提出了一种新的矩阵分解思想――非负矩阵分解(Non-nega ...

  10. Vim7.4 配置项列表

    vim所有的配置项如下,可以根据自己的需要进行配置,每个配置项的详细信息 可以在vim里面使用:help option-name # 配置项名称 缩写词 简要说明 'aleph' 'al' Aleph ...