Lecture--9 Sorting

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

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

2/实现

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

 1 public void bubbleSort(int[] nums) {
 2        for (int i = 0; i < nums.length; i++) {
 3          for (int j = 1; j < nums.length - i; j++) {
 4              if (num[j - 1] > nums[j]) {
 5                  swap(nums, j - 1, j);
 6              }
 7          }
 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 }

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

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

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

 1 public void insertSort(int[] nums) {
 2     for (int i = 1; i < nums.length; i++) {
 3         for (int j = i; j > 0; j --) {
 4             if (nums[j - 1] > nums[j ]) {
 5                 swap(nums, j - 1; j);
 6             }
 7         }
 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 }

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

 1 public void selectionSort(int[] nums) {
 2    for (int i = 0; i < nums.length - 1; i++) {//i < nums.length -1,j要从i + 1开始
 3        int min = nums[i];
 4        int minIndex = i;
 5         for (int j = i + 1; j > nums.length; j++) {
 6             if (num[j] < min) {
 7                 min = num[j];
 8                 minIndex = j;
 9             }
10         }
11         num[minIndex] = num[i];
12         nums[i] = min;
13    }
14 }

4)快速排序quick sort:

 1 public void quickSort(int[] nums) {
 2     sort(nums, 0, nums.length);
 3 }
 4
 5 publc void sort(int[] nums, int begin, int end) {
 6     if (begin >= end) {
 7         return;
 8     }
 9     int pivotIndex = partition(nums, begin, end);
10     sort(nums, begin, pivotIndex - 1);//pivot已经就位,所以跳过
11     sort(nums, pivotIndex + 1, end);
12 }
13
14 public partition(int[] nums, int begin, int end) {
15     int pivot = num[begin];
16     while (begin < end) {
17         while (begin < end && num[end] > pivot) {
18              end--;
19         }
20         nums[begin] = nums[end];
21         while (begin < end && nums[begind] <= pivot) {
22               begin++;
23         }
24         nums[end] = nums[begin];
25     }
26     nums[begin] = pivot;
27     return begin;
28 }        
时间: 2024-11-08 23:16:08

Lecture--9 Sorting的相关文章

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

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

1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到不同的层,便于相机拣选,在相机的cullmask中可以选择渲染哪些层,不选择的层则不会渲染.还可以用于射线检测对象的拣选,可以指定只对某些层的对象进行射线检测. 2,canvas默认是屏幕空间的2D对象,在屏幕空间时仅具有sort order属性,当把它设置为世界空间时,sort order属性消失

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

hdu 2838 Cow Sorting 树状数组

hdu2838 ------希望30号驾校科目一顺利考完,4月即将过去说好的30篇博客也没完成, 真是忙起来就会烦躁什么都不想做,勿忘心安.... <Cow Sorting> 这题本来兴高采烈的想用java做一遍,结果做出来之后无限超内存,真是啊,做题的时候java这种东西还是轻易不要动了.还有感觉要把数字都要离散化的,结果后台数据不需要离散化. 题意:给一个n代表n个牛,然后再给n个数我觉得是n以内的数(包括n).虽然体面上没说.然后只能相邻两个数字交换位置,会让牛产生怒气值,值为互相移动的

codeforces 499B.Lecture 解题报告

题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 professor's lecture 的 n 个单词.问记下来的笔记是什么.对于professor's lecture 的某个单词,如果在单词表中找到,word1, word2 都有可能.如果 word1 的长度  <= word2 的长度,就输出word1,否则word2 考了map<string, st

K.Bro Sorting(杭电5122)(2014ACM/ICPC亚洲区北京站)

K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 67    Accepted Submission(s): 39 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubbl

WebGrid with filtering, paging and sorting 【转】

WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A few days ago I received some questions on the use of the Webgrid helper in the comments section of my personal blog, specifically on how to implement

ASP.NET MVC WebGrid &ndash; Performing true AJAX pagination and sorting 【转】

ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebGrid is a very powerful HTML helper component introduced with ASP.NET MVC 3 and ASP.NET Web Pages. Despite all its cool features, it is troublesome to

[DS Basics] Sorting

Time complexity: Binary search O(log2 n): i=0.   n elements:         ------------------- i=1.   n/2 elements:                   ---------- i=2.   n/4 elements:                          ----- ... i=i.    n/2^i elements:                        - 进行n/2^