Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Time Complexity: O(N^2)
for i ← 1 to length(A) - 1
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
Bubble sort has worst-case and average complexity both О(n2)
First Pass:
( 5 1 4 2 8 )
( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 )
( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 )
( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 )
( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 )
( 1 4 2 5 8 )
( 1 4 2 5 8 )
( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
( 1 2 4 5 8 )
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists
Time Complexty: O(N^2)
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
or
/* a[0] to a[n-1] is the array to sort */
int i,j;
int iMin; /* advance the position through the entire array */
/* (could do j < n-1 because single element is also min element) */
for (j = 0; j < n-1; j++) {
/* find the min element in the unsorted a[j .. n-1] */ /* assume the min is the first element */
iMin = j;
/* test against elements after j to find the smallest */
for ( i = j+1; i < n; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
} if(iMin != j) {
swap(a[j], a[iMin]);
} }
Descending Order:
public class descending {
public static int[] doSelectionSort(int[] arr){
for (int i = arr.length-1; i >= 1; i--)
{
int index = i;
for (int j = 0; j < i; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String[] args){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
Merge Sort:
package ArrayMergeSort;
import java.util.Arrays;
public class Solution {
public int[] mergeSort(int[] arr) {
if (arr.length == 1) return arr;
else {
int[] arr1 = Arrays.copyOfRange(arr, 0, arr.length/2);
int[] arr2 = Arrays.copyOfRange(arr, arr.length/2, arr.length);
return merge(mergeSort(arr1), mergeSort(arr2));
}
}
public int[] merge(int[] arr1, int[] arr2) {
int len1 = arr1.length;
int len2 = arr2.length;
int[] res = new int[len1+len2];
int i = 0, j=0, cur=0;
while (i<len1 && j<len2) {
if (arr1[i] <= arr2[j]) {
res[cur++] = arr1[i++];
}
else {
res[cur++] = arr2[j++];
}
}
while (i<len1) {
res[cur++] = arr1[i++];
}
while (j<len2) {
res[cur++] = arr2[j++];
}
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int[] arr = sol.mergeSort(new int[]{6,5,4,8,2,1});
System.out.println(Arrays.toString(arr));
}
}
Summary: sorting Algorithms的更多相关文章
- JavaScript 排序算法(JavaScript sorting algorithms)
JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Basic Sorting Algorithms
*稳定指原本数列中相同的元素的相对前后位置在排序后不会被打乱 快速排序(n*lgn 不稳定):数组中随机选取一个数x(这里选择最后一个),将数组按比x大的和x小的分成两部分,再对剩余两部分重复这个算法 ...
- 算法的稳定性(Stability of Sorting Algorithms)
如果具有同样关键字的纪录的在排序前和排序后相对位置保持不变.一些算法本身就是稳定的,如插入排序,归并排序,冒泡排序等,不稳定的算法有堆排序,快速排序等. 然而,一个本身不稳定的算法通过一点修正也能变成 ...
- Sorting Algorithms
Merge sort by using recursive strategy, i.e. divide and conquer. def merge(left,right): result = [] ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- [zt]Which are the 10 algorithms every computer science student must implement at least once in life?
More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...
- Top 10 Algorithms for Coding Interview--reference
By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
随机推荐
- Android ADB命令?这一次我再也不死记了!【简单说】
https://www.jianshu.com/p/56fd03f1aaae adb的全称为Android Debug Bridge.是android司机经常用到的工具.但是问题是那么多命令写代码已经 ...
- java(9)并发编程
整理自<java 并发编程的艺术> 1. 上下文切换 即使是单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制.时间片是CPU分配给各个线程的时间,因为时间 ...
- goldengate 过滤对某张表的复制操作
在复制进程中配置下面的参数可以实现对一个用户下的某些表进行过滤,在复制的时候 不做任何操作. MAPEXCLUDE: Valid for Replicat Use the MAPEXCLUDE par ...
- G711算法学习
采样和量化 首先需要明确的两个概念,“采样”和“量化”.对于给定的一个波形,采样是从时间上将连续变成离散的过程,而采样得到的值,可能还是不能够用给定的位宽(比如8bit)来表示,这就需要经过量化,即从 ...
- create-react-app中img图片不现实
场景:正常的情况下是这么引用图片,我的图片路径是 src/images/login-from-icon1.png <img src="../images/login-from-icon ...
- javaagent
-javaagent:<jarpath>[=<options>]load Java programming language agent, see java.lang.inst ...
- 【CF819D】Mister B and Astronomers EXGCD
[CF819D]Mister B and Astronomers 题意:小鼠Jack想当太空人(哦不,太空鼠)!为此,它在夜晚带领一堆小朋友一起来到户外看星星.一共有 $n$ 只小鼠,这些小鼠围成一 ...
- Unity3D笔记 切水果二 刀光剑影
一.步骤一创建一个空GameObject.js 二.代码 #pragma strict var myColor:Color; var firstPosition:Vector3;//鼠标点击的第一个点 ...
- 9.4Django
2018-9-4 17:19:13 昨天和基友玩到了十点 一路溜着玩喝豆腐脑,谈谈人生!感触颇深! 越努力,越幸运! 关于 Django的一开始的配置东西! 2018-9-4 19:42:27 201 ...
- 【转】.NET 应用程序是怎么运行的
原文:http://www.cnblogs.com/xishuai/p/mono-dotnetcore.html .NET应用程序运行过程 C#程序运行过程 CLR结构