Advanced Sort Algorithms
1. Merge Sort
public class Mergesort {
private int[] numbers;
private int[] helper;
private int number;
public void sort(int[] values) {
this.numbers = values;
number = values.length;
this.helper = new int[number];
mergesort(0, number - 1);
}
private void mergesort(int low, int high) {
if (low < high) {
int middle = low + (high - low) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}
private void merge(int low, int middle, int high) {
for (int i = low; i <= high; i++) {
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}
while (i <= middle) {
numbers[k] = helper[i];
k++;
i++;
}
}
}
Performance
Worst case performance O(n log n)
Best case performance O(n log n) typical, O(n) natural variant
Average case performance O(n log n)
Worst case space complexity O(n) auxiliary
2. Shell Sort
public static void main(String[] args) {
int[]a={49,38,65,97,76,13,27,49,78,34,12,64,1};
System.out.println("Beforeing:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"");
}
int d=a.length;
while(true) {
d=d/2;
for(int x=0;x<d;x++){
for(int i=x+d;i<a.length;i=i+d){
int temp=a[i];
int j;
for(j=i-d;j>=0&&a[j]>temp;j=j-d){
a[j+d]=a[j];
}
a[j+d]=temp;
}
}
if(d==1){
break;
}
}
System.out.println();
System.out.println("Aftering:");
for(inti=0;i<a.length;i++){
System.out.print(a[i]+"");
}
}
Performance
Worst case performance O(n2)
Best case performance O(n log n)
Average case performance depends on gap sequence
Worst case space complexity О(n) total, O(1) auxiliary
Comparison
The Shellsort is good for medium-sized arrays, perhaps up to a few thousand items, depending on the particular implementation. It’s not quite as fast as quicksort and other O(N*logN) sorts, so it’s not optimum for very large files. However, it’s much faster than the O(N2) sorts like the selection sort and the insertion sort, and it’s very easy to implement.
3. Quick Sort
public class Quicksort {
private int[] numbers;
private int number;
public void sort(int[] values) {
if (values ==null || values.length==0){
return;
}
this.numbers = values;
number = values.length;
quicksort(0, number - 1);
}
private void quicksort(int low, int high) {
int i = low, j = high;
int pivot = numbers[low + (high-low)/2];
while (i <= j) {
while (numbers[i] < pivot) {
i++;
}
while (numbers[j] > pivot) {
j--;
}
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
private void exchange(int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
Performance
Worst case performance O(n2)
Best case performance O(n log n) (simple partition) or O(n) (three -way partition and equal keys)
Average case performance O(n log n)
Worst case space complexity O(n) auxiliary (naive) O(log n) auxiliary (Sedgewick 1978)
Comparison
Quicksort is undoubtedly the most popular sorting algorithm, and for good reason: In the majority of situations, it’s the fastest, operating in O(N*logN) time. (This is only true for internal or in-memory sorting; for sorting data in disk files, other algorithms may be better.)
4. Radix Sort
public void radixsort(int[] input) {
final int RADIX = 10;
List<Integer>[] bucket = new ArrayList[RADIX];
for (int i = 0; i < bucket.length; i++) {
bucket[i] = new ArrayList<Integer>();
}
boolean maxLength = false;
int tmp = -1, placement = 1;
while (!maxLength) {
maxLength = true;
for (Integer i : input) {
tmp = i / placement;
bucket[tmp % RADIX].add(i);
if (maxLength && tmp > 0) {
maxLength = false;
}
}
int a = 0;
for (int b = 0; b < RADIX; b++) {
for (Integer i : bucket[b]) {
input[a++] = i;
}
bucket[b].clear();
}
placement *= RADIX;
}
}
Performance
Worst case performance O(kN)
Worst case space complexity O(k + N)
Comparison
Of course, like mergesort, the radix sort uses about twice as much memory as quicksort. It’s generally true that if you have more data items, you’ll need longer keys. If you have 10 times as much data, you may need to add another digit to the key. The number of copies is proportional to the number of data items times the number of digits in the key. The number of digits is the log of the key values, so in most situations we’re back to O(N*logN) efficiency, the same as quicksort.
Advanced Sort Algorithms的更多相关文章
- Basic Sort Algorithms
1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...
- sort algorithms
//todo #include<iostream> void swap(int *a, int *b){int temp = *a; *a = *b; *b = temp;} ; i &l ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- Machine Learning - 第3周(Logistic Regression、Regularization)
Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...
- [C4] Andrew Ng - Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization
About this Course This course will teach you the "magic" of getting deep learning to work ...
- [C2P2] Andrew Ng - Machine Learning
##Linear Regression with One Variable Linear regression predicts a real-valued output based on an in ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- Bayesian machine learning
from: http://www.metacademy.org/roadmaps/rgrosse/bayesian_machine_learning Created by: Roger Grosse( ...
- Javascript数组算法和技巧总结
Js-arrayMethod https://github.com/HerbertKarajan/Js-arrayMethod List unique an array 数组去重 random str ...
随机推荐
- 老李分享:Web Services 组件 1
老李分享:Web Services 组件 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9 ...
- smartgit试用到期不用序列号怎么继续使用
方法1 1:进入C盘,搜索SmartGit,出现\AppData\Roaming\syntevo: 2:进入文件7.1和8,这个有不同的版本可以使用,上次里面的setting.xml文件 3:重新打开 ...
- [Linux] PHP程序员玩转Linux系列-Nginx中的HTTPS
1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...
- Centos安装MySql、Java、Tomcat
一.安装MySql 安装MySql yum install -y mysql-server mysql mysql-devel 启动MySql服务 service mysqld start 为root ...
- MySQL读写分离技术
1.简介 当今MySQL使用相当广泛,随着用户的增多以及数据量的增大,高并发随之而来.然而我们有很多办法可以缓解数据库的压力.分布式数据库.负载均衡.读写分离.增加缓存服务器等等.这里我们将采用读写分 ...
- .NET Core 2.0及.NET Standard 2.0
.NET Core 2.0的发布时间,.NET Core 2.0预览版及.NET Standard 2.0 Preview大概在5月中旬或下旬发布. .NET Core 2.0正式版本发布时间大约在Q ...
- 第十章 MyBatis入门
第十章 MyBatis入门10.1 MyBatis入门 优点:简单且功能强大.能够完全控制SQL语句.容易维护和修改 缺点:移植性不好 使用步骤: 1.下载 ...
- Named function expressions demystified
Introduction Surprisingly, a topic of named function expressions doesn't seem to be covered well eno ...
- ASP.NET MVC5请求管道和生命周期
请求处理管道 请求管道是一些用于处理HTTP请求的模块组合,在ASP.NET中,请求管道有两个核心组件:IHttpModule和IHttpHandler.所有的HTTP请求都会进入IHttpHandl ...
- WEB认证模式:Basic & Digest
一. HTTP Basic 客户端以" : "连接用户名和密码后,再经BASE64加密通过Authorization请求头发送该密文至服务端进行验证,每次请求都需要重复发送该密文. ...