System.out.print( arr_1[i] +" ");
System.out.println("");
*/
}
}
class Sort {
public void swap(int arr[],int a, int b) {
int t;
t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
public void Select_sort(int[] arr) {
int temp,index;
for( int i=0;i10;i++) {
index = i;
for(int j = i + 1 ; j10 ; j++) {
if(arr[j]arr[index])
index = j;
}
/*
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
*/
swap(arr,i,index);
}
System.out.print("经过选择排序后:");
for(int i = 0 ; i10 ; i++)
System.out.print( arr[i] +" ");
System.out.println("");
}
public void Bubble_sort(int[] arr) {
int temp;
for(int i = 0 ; i9 ; i++) {
for(int j = 0 ; j10 - i - 1 ;j++) {
if(arr[j]arr[j+1]) {
/*
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
*/
swap(arr,j,j+1);
}
}
}
System.out.print("经过冒泡排序后:");
for(int i = 0 ; i10 ; i++)
System.out.print( arr[i] +" ");
System.out.println("");
}
public void Quick_sort(int[] arr, int left, int right) {
if(left = right)
return ;
int temp,t;
int j = right;
int i = left;
temp = arr[left];
while(ij) {
while(arr[j] = tempij)
j--;
while(arr[i] = tempij)
i++;
if(ij) {
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
arr[left] = arr[i];
arr[i] = temp;
Quick_sort(arr,left, i - 1);
Quick_sort(arr, i + 1, right);
}
public void Insert_sort(int[] arr) {
int length = arr.length;
int[] arr_sort = new int[length];
int count = 0;
for(int i = 0;ilength; i++) {
if(count == 0) {
arr_sort[0] = arr[0];
}else if(arr[i] = arr_sort[count - 1]) {
arr_sort[count] = arr[i];
}else if(arr[i]arr_sort[0]) {
insert(arr,arr_sort,arr[i],0,count);
}else {
for(int j = 0;jcount - 1; j++) {
if(arr[i] = arr_sort[j]arr[i]arr_sort[j+1]) {
insert(arr,arr_sort,arr[i],j+1,count);
break;
}
}
}
count++;
}
System.out.print("经过插入排序后:");
for(int i = 0 ; i10 ; i++)
System.out.print( arr_sort[i] +" ");
System.out.println("");
}
public void insert(int[] arr,int[] arr_sort,int value,int index,int count) {
for(int i = count; iindex; i--)
arr_sort[i] = arr_sort[i-1];
arr_sort[index] = value;
}
public void Mergesort(int[] arr,int left,int right) {
if(right - left0) {
int[] arr_1 = new int[(right - left)/2 + 1];
int[] arr_2 = new int[(right - left + 1)/2];
int j = 0;
int k = 0;
for(int i = left;i = right;i++) {
if(i = (right + left)/2) {
arr_1[j++] = arr[i];
}else {
arr_2[k++] = arr[i];
}
}
Mergesort(arr_1,0,(right - left)/2);
Mergesort(arr_2,0,(right - left - 1)/2);
Merge(arr_1,arr_2,arr);
}
}
public void Merge(int[] arr_1,int[] arr_2,int[] arr) {
int i = 0;
int j = 0;
int k = 0;
int L1 = arr_1.length;
int L2 = arr_2.length;
while(iL1jL2) {
if(arr_1[i] = arr_2[j]) {
arr[k] = arr_1[i];
i++;
}else {
arr[k] = arr_2[j];
j++;
}
k++;
}
if(i == L1) {
for(int t = j;jL2;j++)
arr[k++] = arr_2[j];
}else {
for(int t = i;iL1;i++)
arr[k++] = arr_1[i];
}
}
}
若有错误,麻烦指正,不胜感激 。
java快速排序简单代码.example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px}排序算法是《数据结构与算法》中最基本的算法之一 。排序算法可以分为内部排序和外部排序 , 内部排序是数据记录在内存中进行排序 , 而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存 。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等 。以下是快速排序算法:
推荐阅读
- 兼容sqlserver7,兼容模式怎么设置
- jquery插件调用方法,jQuery插件下载
- Linux的Ex命令,linux中exec
- 怎么样微信开直播带货,怎么样微信开直播带货卖货
- go语言内存泄露排查 go语言内存不断升高
- jquery卡片展现数据,jquery选项卡
- 电商运营如何谈快递,电商运营如何谈快递业务
- 剑术格斗游戏视频,剑术格斗教学视频
- php原生插入数据库 php数据库写入实例