• 冒泡排序
public class BubbleSort{

	public static int[] asc(int[] a){
int item;
for (int i = 0; i < a.length-1; i++) {
item = a[i];
for (int j = i+1; j < a.length; j++) {
if (a[j]<a[i]) {
a[i] = a[j];
a[j] = item;
item = a[i];
}
}
}
return a;
} public static int[] desc(int[] a){
int item;
for (int i = 0; i < a.length-1; i++) {
item = a[i];
for (int j = i+1; j < a.length; j++) {
if (a[j]>a[i]) {
a[i] = a[j];
a[j] = item;
item = a[i];
}
}
}
return a;
} public static void main(String[] args) {
int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
a = asc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("\n");
}
}
  • 堆排序
public class HeapSort{

	public static int[] asc(int[] a){
a = buildHeap(a);
int item;
for (int i = a.length-1; i >=1; i--) {
item = a[0];
a[0] = a[i];
a[i] = item;
heapAdjust(a,0,i-1);
}
return a;
} private static int[] buildHeap(int[] a) {
return buildHeap(a,true);
} private static int[] buildHeap(int[] a,boolean asc) {
for (int i = a.length/2; i >= 0; i--) {
a = heapAdjust(a,i,a.length-1,asc);
}
return a;
} public static int[] heapAdjust(int[] a,int index,int size){
return heapAdjust(a, index, size, true);
} public static int[] heapAdjust(int[] a,int index,int size,boolean asc){
int left = index*2 + 1;
int right = left + 1;
int max = index;
if (left<=size) {
if (asc&&a[left]>a[max]) {
max = left;
}
if (!asc&&a[left]<a[max]) {
max = left;
}
}
if (right<=size) {
if (asc&&a[right]>a[max]) {
max = right;
}
if (!asc&&a[right]<a[max]) {
max = right;
}
}
if (max != index) {
int temp = a[max];
a[max] = a[index];
a[index] = temp;
heapAdjust(a, max, size, asc);
}
return a;
} public static int[] desc(int[] a){
a = buildHeap(a,false);
int item;
for (int i = a.length-1; i >=1; i--) {
item = a[0];
a[0] = a[i];
a[i] = item;
heapAdjust(a,0,i-1,false);
pump(a);
}
return a;
} public static void main(String[] args) {
int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
a = desc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("\n");
}
}
  • 插入排序
public class InsertSort{

	public static int[] asc(int[] a){
int item;
for (int i = 0; i < a.length-1; i++) {
item = a[i+1];
for (int k = i; k >= 0; k--) {
if (a[k]>item) {
a[k+1] = a[k];
a[k] = item;
}else {
break ;
}
}
}
return a;
} public static int[] desc(int[] a){
int item;
for (int i = 0; i < a.length-1; i++) {
item = a[i+1];
for (int k = i; k >= 0; k--) {
if (a[k]<item) {
a[k+1] = a[k];
a[k] = item;
}else {
break ;
}
}
}
return a;
} public static void main(String[] args) {
int a[] = {23,14,25,32,18,27,15,13,58,8,2,36};
a = desc(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
  • 归并排序
public class MergeSort{

	public static int[] asc(int[] a){
return asc(a,0,a.length-1);
} public static int[] asc(int[] a,int start,int end){
if (start<end) {
if (start+1==end) {
if(a[start]>a[end]){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
return a;
}
int m = (start+end)/2;
a = asc(a,start,m);
a = asc(a,m+1,end);
a = merge(a,start,m,end);
}
return a;
} private static int[] merge(int[] a, int start, int m, int end) {
for (int j = m+1; j <= end; j++) {
int index = j,i=j-1,item = a[j];
while (i >= start&&item<a[i]) {
index = i--;
}
if (index != j) {
for (int k = j; k >index; k--) {
a[k] = a[k-1];
}
a[index] = item;
}
}
return a;
} private static int[] merge(int[] a, int start, int m, int end,boolean asc) {
for (int j = m+1; j <= end; j++) {
int index = j,i=j-1,item = a[j];
if (asc) {
while (i >= start&&item<a[i]) {
index = i--;
}
}else {
while (i >= start&&item>a[i]) {
index = i--;
}
}
if (index != j) {
for (int k = j; k >index; k--) {
a[k] = a[k-1];
}
a[index] = item;
}
}
return a;
} public static int[] desc(int[] a){
return desc(a,0,a.length-1);
} private static int[] desc(int[] a,int start,int end) {
if (start<end) {
if (start+1==end) {
if(a[start]<a[end]){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
return a;
}
int m = (start+end)/2;
a = desc(a,start,m);
a = desc(a,m+1,end);
a = merge(a,start,m,end,false);
}
return a;
} public static void main(String[] args) {
int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
a = desc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("\n");
}
}
  • 快速排序
public class QuickSort {

	public static int[] asc(int[] a, int low, int high) {
if (low >= high || (a[low] <= a[high] && low + 1 == high)) {
return a;
} else if (a[low] > a[high] && low + 1 == high) {
int temp = a[high];
a[high] = a[low];
a[low] = temp;
return a;
}
int tag = a[low], start = low, end = high;
boolean left_right = false;
while (low < high) {
if (!left_right) {
if (a[high] < tag) {
a[low] = a[high];
a[high] = tag;
low++;
left_right = true;
} else {
high--;
}
} else {
if (a[low] > tag) {
a[high] = a[low];
a[low] = tag;
left_right = false;
high--;
} else {
low++;
}
}
}
if (start < high) {
a = asc(a, start, high == low ? high - 1 : high);
}
if (low < end) {
a = asc(a, low == high ? low + 1 : low, end);
}
return a;
} public static int[] desc(int[] a, int low, int high) {
if (low >= high || (a[low] >= a[high] && low + 1 == high)) {
return a;
} else if (a[low] < a[high] && low + 1 == high) {
int temp = a[high];
a[high] = a[low];
a[low] = temp;
return a;
}
int tag = a[low], start = low, end = high;
boolean left_right = false;
while (low < high) {
if (!left_right) {
if (a[high] > tag) {
a[low] = a[high];
a[high] = tag;
low++;
left_right = true;
} else {
high--;
}
} else {
if (a[low] < tag) {
a[high] = a[low];
a[low] = tag;
left_right = false;
high--;
} else {
low++;
}
}
}
if (start < high) {
a = desc(a, start, high == low ? high - 1 : high);
}
if (low < end) {
a = desc(a, low == high ? low + 1 : low, end);
}
return a;
} public static void main(String[] args) {
int a[] = { 23, 14, 25, 32, 8, 27, 15, 13, 58, 8, 2, 36 };
a = desc(a, 0, a.length - 1);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n");
}
}
  • 基数排序
public class RadixSort {

	private static int[] radix(int[] a, int radix, boolean asc) {
int[][] index = new int[10][];
int pow = Double.valueOf(Math.pow(10, radix)).intValue(), bucket = 0;
for (int i = 0; i < a.length; i++) {
bucket = a[i] % pow / (pow / 10);
if (index[bucket] == null) {
index[bucket] = new int[] { a[i] };
} else {
index[bucket] = add(index[bucket], a[i]); }
}
int k = 0;
int[] b = new int[a.length];
if (asc) {
for (int i = 0; i < 10; i++) {
if (index[i] != null && index[i].length > 0) {
System.arraycopy(index[i], 0, b, k, index[i].length);
k += index[i].length;
}
}
} else {
for (int i = 9; i >= 0; i--) {
if (index[i] != null && index[i].length > 0) {
System.arraycopy(index[i], 0, b, k, index[i].length);
k += index[i].length;
}
}
}
return b;
} private static int[] add(int[] a, int v) {
if (a == null || a.length == 0) {
return new int[] { v };
} else {
int[] b = new int[a.length + 1];
System.arraycopy(a, 0, b, 0, a.length);
b[a.length] = v;
return b;
}
} public static int[] asc(int[] a) {
int bucket = 1, max = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] > a[max]) {
max = i;
}
}
bucket = String.valueOf(a[max]).length();
for (int i = 1; i <= bucket; i++) {
a = radix(a, i, true);
}
return a;
} public static int[] desc(int[] a) {
int bucket = 1, max = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] > a[max]) {
max = i;
}
}
bucket = String.valueOf(a[max]).length();
for (int i = 1; i <= bucket; i++) {
a = radix(a, i, false);
}
return a;
} public static void main(String[] args) {
int a[] = { 23, 14, 25, 32, 8, 27, 15, 13, 58, 8, 2, 36 };
a = desc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n");
}
}
  • 选择排序
public class SelectSort{

	public static int[] asc(int[] a){
int index;
for (int i = 0; i < a.length-1; i++) {
index = i;
for (int j = i+1; j < a.length; j++) {
if (a[j]<a[index]) {
index = j;
}
}
if (a[i]>a[index]) {
int item = a[i];
a[i] = a[index];
a[index] = item;
}
}
return a;
} public static int[] desc(int[] a){
int index;
for (int i = 0; i < a.length-1; i++) {
index = i;
for (int j = i+1; j < a.length; j++) {
if (a[j]>a[index]) {
index = j;
}
}
if (a[i]<a[index]) {
int item = a[i];
a[i] = a[index];
a[index] = item;
}
}
return a;
} public static void main(String[] args) {
int a[] = {23,14,25,32,8,27,15,13,58,8,2,36};
a = desc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("\n");
}
}
  • 希尔排序
public class ShellSort {

	public static int[] asc(int[] a) {
int d = (a.length + 1) / 2;
while (d >= 1) {
shell(a, d, true);
d = d / 2;
}
return a;
} private static void shell(int[] a, int d, boolean asc) {
int item;
if (asc) {
for (int i = 0; i < d; i++) {
for (int j = i; j + d < a.length; j += d) {
item = a[j + d];
for (int k = j; k >= 0; k -= d) {
if (a[k] > item) {
a[k + d] = a[k];
a[k] = item;
} else {
break;
}
}
}
}
} else {
for (int i = 0; i < d; i++) {
for (int j = i; j + d < a.length; j += d) {
item = a[j + d];
for (int k = j; k >= 0; k -= d) {
if (a[k] < item) {
a[k + d] = a[k];
a[k] = item;
} else {
break;
}
}
}
}
}
} public static int[] desc(int[] a) {
int d = (a.length + 1) / 2;
while (d >= 1) {
shell(a, d, false);
d = d / 2;
}
return a;
} public static void main(String[] args) {
int a[] = { 23, 14, 25, 32, 18, 27, 15, 13, 58, 8, 2, 36 };
a = desc(a);
pump(a);
} private static void pump(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n");
}
}

java实现内部排序算法的更多相关文章

  1. Java实现各种内部排序算法

    数据结构中常见的内部排序算法: 插入排序:直接插入排序.折半插入排序.希尔排序 交换排序:冒泡排序.快速排序 选择排序:简单选择排序.堆排序 归并排序.基数排序.计数排序 直接插入排序: 思想:每次将 ...

  2. 用 Java 实现常见的 8 种内部排序算法

    一.插入类排序 插入类排序就是在一个有序的序列中,插入一个新的关键字.从而达到新的有序序列.插入排序一般有直接插入排序.折半插入排序和希尔排序. 1. 插入排序 1.1 直接插入排序 /** * 直接 ...

  3. Java中的排序算法(2)

    Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...

  4. java实现各种排序算法

    java实现各种排序算法 import java.util.Arrays; public class SomeSort { public static void main(String[] args) ...

  5. 常见内部排序算法对比分析及C++ 实现代码

    内部排序是指在排序期间数据元素全部存放在内存的排序.外部排序是指在排序期间全部元素的个数过多,不能同时存放在内存,必须根据排序过程的要求,不断在内存和外存之间移动的排序.本次主要介绍常见的内部排序算法 ...

  6. 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)

    排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...

  7. Java实现常见排序算法

    常见的排序算法有冒泡排序.选择排序.插入排序.堆排序.归并排序.快速排序.希尔排序.基数排序.计数排序,下面通过Java实现这些排序 1.冒泡排序 package com.buaa; import j ...

  8. 七内部排序算法汇总(插入排序、Shell排序、冒泡排序、请选择类别、、高速分拣合并排序、堆排序)

    写在前面: 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素的随意序列,又一次排列成一个按keyword有序的序列.因此排序掌握各种排序算法很重要. 对以下介绍的各个排序,我们假定全部排 ...

  9. Java数组的排序算法

    在Java中,实现数组的排序算法有很多,如冒泡排序法.选择排序法.直接插入法和快速排序法等.下面介绍几种排序算法的具体 实现. 本文引用文献:Java必须知道的300个问题. 1.冒泡排序法 1.1 ...

随机推荐

  1. Express中间件

    一.编写中间件 中间件函数能够访问请求对象(req),响应对象(res),应用程序的请求/响应循环中的下一个中间件函数.下一个中间件函数通常由名为next的变量来表示. 中间件函数可以执行以下任务: ...

  2. BZOJ 4726 POI 2017 Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  3. BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:/ ...

  4. 离线下载Xcode的文档

    https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex 找到里面的文档下载地址 例如iOS 8. ...

  5. 长城小主机GW1等型号进BIOS的设置方法

    主板型号 1.进BIOS办法 2.BIOS下设置U盘启动 3.主板设置上电启动 4.主要是否具有快速U盘启动功能 5.定时开机设置 945GMS Ctrl+Alt+F1(注意:自检响铃后,再按) 在B ...

  6. [Android] 字体使用dp单位避免设置系统字体大小对排版的影响

    [Android] 字体使用dp单位避免设置系统字体大小对排版的影响 以魄族mx3为例,在设置->显示->字体大小中能够选择字号大小例如以下图: 图1. 魄族mx3 会导致软件在有固定定高 ...

  7. How to install WP 8.0 SDK if WP 8.1 SDK is installed?

    I have the Windows Phone 8.1 SDK on my dev pc installed together with VS 2013. I need to open some W ...

  8. 自定义圆形控件RoundImageView并认识一下attr

    昨天我们学习了自定义带图片和文字的ImageTextButton,非常简单,我承诺给大家要讲一下用自定义属性的方式学习真正的实现自定义控件,在布局文件中使用属性的方式就需要用到attr.xml这个文件 ...

  9. UCOS移植心得(

    移植UCOS之前,你首先应该做好三件事: 1.弄懂UCOS,这是谁都知道的哦 ^_^ 2. 弄懂你想要移植到的硬件平台 3. 清楚你使用的编译器是如何处理函数的局部变量和怎么样处理函数间的参数传递 这 ...

  10. windows下的Nginx+Squid+Tomcat+Memcached集群