2019独角兽企业重金招聘Python工程师标准>>>

问题描述:

BubbleSort

InsertionSort

ShellSort

MergeSort

HeapSort

QuickSort

问题分析:

时间复杂度?

空间复杂度?

代码实现:

public class BubbleSort {public static <AnyType extends Comparable<? super AnyType>>void bubblesort(AnyType[] a){boolean done = false;int size = a.length;while(!done){done = true;for(int i = 0; i < size - 1; i++){if(a[i].compareTo(a[i+1]) > 0){done = false;AnyType tmp = a[i+1];a[i+1] = a[i];a[i] = tmp;}}size--;}}
}
class InsertionSort {public static <AnyType extends Comparable<? super AnyType>>void insertionSort(AnyType[] a) {for (int p = 1; p < a.length; p++) {AnyType tmp = a[p];//需找合适的插入位置int j;for (j = p; j > 0 && tmp.compareTo(a[j-1]) < 0; j--)a[j] = a[j - 1];a[j] = tmp;}}
}
public class ShellSort {public static <AnyType extends Comparable<? super AnyType>>void shellsort(AnyType[] a) {for(int gap = a.length/2; gap > 0; gap /= 2)for(int i = gap; i < a.length; i++){AnyType tmp = a[i];int j = i;for(; j >= gap && tmp.compareTo(a[j-gap]) < 0; j -= gap)a[j] = a[j - gap];a[j] = tmp;}}
}
public class MergeSort {public static <AnyType extends Comparable<? super AnyType>>void mergesort(AnyType[] a){AnyType[] tmp = (AnyType[])new Comparable[a.length];mergesort(a, tmp, 0, a.length - 1);}public static <AnyType extends Comparable<? super AnyType>>void mergesort(AnyType[] a, AnyType[] tmp, int left, int right){if(left < right){int center  = (left+right)/2;mergesort(a, tmp, left, center);mergesort(a, tmp, center + 1,right);merge(a, tmp, left, center + 1, right);}}public static <AnyType extends Comparable<? super AnyType>>void merge(AnyType[] a, AnyType[] tmp, int leftPos, int rightPos, int rightEnd){int leftEnd = rightPos - 1;int tmpPos = leftPos;int numElements = rightEnd - leftPos + 1;while(leftPos <= leftEnd && rightPos <= rightEnd){if(a[leftPos].compareTo(a[rightPos]) < 0)tmp[tmpPos++] = a[leftPos++];elsetmp[tmpPos++] = a[rightPos++];}while(leftPos <= leftEnd)tmp[tmpPos++] = a[leftPos++];while(rightPos <= rightEnd)tmp[tmpPos++] = a[rightPos++];for(int i = 0; i < numElements; i++, rightEnd--)a[rightEnd] = tmp[rightEnd];}
}
public class HeapSort {static int leftChild(int i){return 2*i + 1;}public static <AnyType extends Comparable<? super AnyType>>void heapsort(AnyType[] a){for(int i = a.length/2; i >= 0; i--)percDown(a, i, a.length);for(int j = a.length - 1; j > 0; j--){AnyType tmp = a[0];a[0] = a[j];a[j] = tmp;percDown(a, 0, j);}}public static <AnyType extends Comparable<? super AnyType>>void percDown(AnyType[] a, int i, int n){int child;AnyType tmp;for(tmp = a[i]; leftChild(i) < n; i = child){child = leftChild(i);if(child != n -1 && a[child].compareTo(a[child + 1]) < 0)child++;if(tmp.compareTo(a[child]) < 0)a[i] = a[child];elsebreak;}a[i] = tmp;}
}
public class QuickSort {public static <AnyType extends Comparable<? super AnyType>>void quicksort(AnyType[] a){quicksort(a, 0, a.length - 1);}private static <AnyType extends Comparable<? super AnyType>>AnyType median3(AnyType[]a , int left, int right){int center = (left + right)/2;if(a[center].compareTo(a[left]) < 0){AnyType tmp = a[center];a[left] = a[center];a[center] = tmp;}if(a[right].compareTo(a[left]) < 0){AnyType tmp = a[right];a[right] = a[left];a[left] = tmp;}if(a[right].compareTo(a[center]) < 0){AnyType tmp = a[right];a[right] = a[center];a[center] = tmp;}AnyType temp = a[center];a[center] = a[right - 1];a[right - 1] = temp;return a[right - 1];}static <AnyType extends Comparable<? super AnyType>>void quicksort(AnyType[] a, int left, int right){if(left + 10 < right){AnyType pivot = median3(a, left, right);int i = left, j = right - 1;for(;;){while(a[++i].compareTo(pivot) < 0 ){}while(pivot.compareTo(a[--j]) < 0){}if(i < j){AnyType tmp = a[i];a[i] = a[j];a[j] = tmp;} elsebreak;}AnyType tmp = a[i];a[i] = a[right - 1];a[right - 1] = tmp;quicksort(a, left, i - 1);quicksort(a, i + 1, right);}elseinsertionSort(a);}
}
public class SortTest {public static void main(String[] args) {Integer[] v = {9,3,4,5,1,6,8,2,12,14,22,18,3,11};InsertionSort.insertionSort(v);//BubbleSort.bubblesort(v);//ShellSort.shellsort(v);//HeapSort.heapsort(v);//MergeSort.mergesort(v);//QuickSort.quicksort(v);for (int i = 0; i < v.length; i++) {Integer integer = v[i];System.out.print(integer + " ");}}
}

转载于:https://my.oschina.net/jimmyhan/blog/521870

基本数据结构之Sort相关推荐

  1. Golang sort 排序

    1.前言 开发过程中,我们经常需要对元素进行排序,使用 Go 我们可以轻松实现. Go 内置 sort 包中提供了根据一些排序函数可对任何序列进行排序,并提供自定义排序规则的能力. sort 包实现了 ...

  2. g++ -std=c++_在C ++ std库中使用sort()

    g++ -std=c++ 介绍 (Introduction) Hey there! Today we are going to discuss the sort() function in the s ...

  3. 数据结构:01 概念与结构

    起源 ① 1968年美国的高德纳教授发表了<计算机程序设计艺术>第一卷<基本算法>开创了数据结构与算法的先河 ② 数据结构是一门研究数据之间关系和操作的学科,而非计算方法 ③ ...

  4. Go中的三种排序方法

    排序操作是很多程序经常使用的操作.尽管一个简短的快排程序只要二三十行代码就可以搞定,但是一个健壮的实现需要更多的代码,并且我们不希望每次我们需要的时候都重写或者拷贝这些代码.幸运的是,Go内置的sor ...

  5. Golang 语言的排序、结构体排序

    文章目录 1.前言 2.基本类型切片排序 3.自定义比较器 4.排序任意数据结构 5.小结 1.前言 开发过程中,我们经常需要对元素进行排序,使用 Go 我们可以轻松实现. Go 内置 sort 包中 ...

  6. Python入门篇-数据结构堆排序Heap Sort

    Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树每个非叶子结点都要大于或者等于其左右孩子结点的 ...

  7. 7.使用php描述冒泡排序,PHP 数据结构 算法描述 冒泡排序 bubble sort

    PHP 数据结构 算法描述 冒泡排序 bubble sort 复制代码 代码如下: /** * 冒泡排序 bubble sort * * 原理:多次循环进行比较,每次比较时将最大数移动到最上面.每次循 ...

  8. 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)

    树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...

  9. SDUT 3400 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...

最新文章

  1. 2、String相关问题
  2. python身份运算符的语法规则_7 Python语法入门之与用户交互、运算符
  3. 永远记住9月26号这一天!
  4. 全国计算机网络自学考试,2008年1月全国自考“计算机网络基本原理”试题
  5. mac+修改+ssh文件夹权限_用SSH指令批量修改文件夹 文件权限和拥有者
  6. GDCM:gdcm::String的测试程序
  7. 配置SMB共享 、 配置NFS共享
  8. Linux编程里getopt_long_only函数用法详解
  9. 企业微信_新建自建H5小程序应用及主页与菜单设置
  10. json vue 对象转数组_vue.js基于v-for实现批量渲染 Json数组对象列表数据示例
  11. [NIO]dawn之Task具体解释
  12. STM8L101+si4463低功耗和自动唤醒配置
  13. 新一代ARINC818仿真板卡
  14. java中file系统找不到指定的路径_java.io.FileNotFoundException: (系统找不到指定的路径。)怎么处理...
  15. php Spreadsheet Csv,基于 PhpSpreadsheet 简单 Excel 导入导出
  16. 【C语言程序】编写函数返回体温值
  17. android 播放器 sma,SMA测评软件下载-SMA测评安卓版v1.1.2-魅卓网
  18. Burp Suite win10下安装图文教程
  19. Android的GridView和ScrollView的嵌套
  20. 服务器意外断电的数据恢复过程

热门文章

  1. SQL2005 远程连接问题解决方法
  2. Android 完全退出应用程序实现代码
  3. AndroidStudio_安卓原生开发_sharedpreferences清空---Android原生开发工作笔记150
  4. ES6新特性_ES6函数参数的默认值设置---JavaScript_ECMAScript_ES6-ES11新特性工作笔记011
  5. Linux工作笔记030---Centos7.3启动tomcat 输入startup.sh后提示command not found
  6. Linux学习笔记010---CentOS7下安装FTP服务
  7. linux下如何添加一个用户并且让用户获得root权限
  8. c++中,可以用delete释放一个数组的空间吗?
  9. ASCII与unicode的转换
  10. linux内核配置成qspi挂载,Zynq-Linux移植学习笔记之十-qspi驱动配置