关于排序算法的自我实现
1冒泡排序以及快速排序
冒泡排序即是一种循环找寻最大值的过程,每经过一次循环,找出一个最大值放在末尾,内
层循环数减一,直到最后,所以对于冒泡排序适合排列那种混乱值小的那种数列,也可以在
内层循环体内设置计数器,若是计数器为0,则没有进行交换则数列本就有序,无需再进行
后续重复无用的排序!!
package com.java.lianxi.zql;
public class Popsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}//随机产生一个数组
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
System.out.println();
int tag = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length-i-1; j++) {
if(a[j]>a[j+1]){
int temp;
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
tag++;
}
}
if(tag == 0)
break;
}
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
}
}
快速排序
快速排序即是 将目标数列分成n个小数列,首先我们设定一个标志位temp,方便把以后的
数据整理成左边的数据都比temp小,右边的数据都比temp大,以及再设定两个标志位指向数
组的最低位low以及最高位high,当循环开始时从high开始,high--若有,a[high]<temp的
数据将a[low] = a[high];此刻循环进入低端,然后low++,比较a[low]与temp的大小若是
出现a[low]>temp的将 a[high] = a[low];这样一直进行下去,就会得到两组数据,第一组
小于temp,第二组大于temp位,然后再进行上诉操作,直到low=high为止。
public class Quicksort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}//随机产生一个数组
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
System.out.println();
_quickSort(a,0,9);
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
}
public static int getMiddle(int[] a,int low,int high){
int temp = a[low];
while(low<high){
while(low<high && a[high]>temp){
high--;
}
a[low] = a[high];
while(low<high && a[low]<temp){
low++;
}
a[high] = a[low];
}
a[low] = temp;
return low;
}
public static void _quickSort(int[]a,int low,int high){
if(low<high && a.length>0){
int middle = getMiddle(a,low,high);
_quickSort(a,low,middle-1);
_quickSort(a,middle+1,high);
}
}
}
2插入排序以及希尔排序
插入排序是一种一个元素一个元素的插入有序序列的过程,比如一个a[10]数列,首先将a
[0]设置为哨兵位,循环从2(i)开始 开始比较a[1] a[2]的大小若是a[2]=>a[1]则循环继续
循环不需要进入循环体,反之进入循环体,先将a[2]的数值保存在哨兵位a[0]上,接着将a
[2]的值赋给a[1],然后循环比较a[0](即a[2])的值与a[i-2]的值得大小,这里i取2,则无
需进行比较数列已经有序,当i大于2以后则需要比较a[i]与a[i-2]的大小若是小于a[i-2]则
把a[i-2]的数据后移一位,到最后,若没有则将a[0]的值插入a[i-1]处.....
希尔排序则是一种 涉及步长的 插入排序,所以在给定一个数列时候选择步长尤为重要,
保证每一个数字在每一次步长里面最为快速..
package com.java.lianxi.zql;
public class Shellsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
ShellInsort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
public static void ShellInsort(int[] a){
int[] b = {4,2,1};
for (int i = 0; i < b.length; i++) {
Insert(a,b[i]);
}
}
public static void Insert(int[] a,int nrc){
int Inserpos = 0;
for (int i = 1+nrc; i < a.length; i++) {
if(a[i]<a[i-nrc]){
a[0]=a[i];
for (int k = i-nrc; k >= 0 ; k-=nrc) {
if(a[0]<a[k]){
a[k+nrc]=a[k];
if(k-nrc <= 0)
Inserpos=k;
}else{
Inserpos = k+nrc;
break; //因为前面是有序的 因此当检测到了 a[0]> = a[k]立即跳出循环!
}
}
a[Inserpos]=a[0];
a[0] = 0;
}
}
}
}
1冒泡排序以及快速排序
冒泡排序即是一种循环找寻最大值的过程,每经过一次循环,找出一个最大值放在末尾,内
层循环数减一,直到最后,所以对于冒泡排序适合排列那种混乱值小的那种数列,也可以在
内层循环体内设置计数器,若是计数器为0,则没有进行交换则数列本就有序,无需再进行
后续重复无用的排序!!
package com.java.lianxi.zql;
public class Popsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}//随机产生一个数组
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
System.out.println();
int tag = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length-i-1; j++) {
if(a[j]>a[j+1]){
int temp;
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
tag++;
}
}
if(tag == 0)
break;
}
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
}
}
快速排序
快速排序即是 将目标数列分成n个小数列,首先我们设定一个标志位temp,方便把以后的
数据整理成左边的数据都比temp小,右边的数据都比temp大,以及再设定两个标志位指向数
组的最低位low以及最高位high,当循环开始时从high开始,high--若有,a[high]<temp的
数据将a[low] = a[high];此刻循环进入低端,然后low++,比较a[low]与temp的大小若是
出现a[low]>temp的将 a[high] = a[low];这样一直进行下去,就会得到两组数据,第一组
小于temp,第二组大于temp位,然后再进行上诉操作,直到low=high为止。
public class Quicksort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}//随机产生一个数组
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
System.out.println();
_quickSort(a,0,9);
for (int i = 0; i < a.length; i++) {
System.out.print(" "+a[i]);
}
}
public static int getMiddle(int[] a,int low,int high){
int temp = a[low];
while(low<high){
while(low<high && a[high]>temp){
high--;
}
a[low] = a[high];
while(low<high && a[low]<temp){
low++;
}
a[high] = a[low];
}
a[low] = temp;
return low;
}
public static void _quickSort(int[]a,int low,int high){
if(low<high && a.length>0){
int middle = getMiddle(a,low,high);
_quickSort(a,low,middle-1);
_quickSort(a,middle+1,high);
}
}
}
2插入排序以及希尔排序
插入排序是一种一个元素一个元素的插入有序序列的过程,比如一个a[10]数列,首先将a
[0]设置为哨兵位,循环从2(i)开始 开始比较a[1] a[2]的大小若是a[2]=>a[1]则循环继续
循环不需要进入循环体,反之进入循环体,先将a[2]的数值保存在哨兵位a[0]上,接着将a
[2]的值赋给a[1],然后循环比较a[0](即a[2])的值与a[i-2]的值得大小,这里i取2,则无
需进行比较数列已经有序,当i大于2以后则需要比较a[i]与a[i-2]的大小若是小于a[i-2]则
把a[i-2]的数据后移一位,到最后,若没有则将a[0]的值插入a[i-1]处.....
希尔排序则是一种 涉及步长的 插入排序,所以在给定一个数列时候选择步长尤为重要,
保证每一个数字在每一次步长里面最为快速..
package com.java.lianxi.zql;
public class Shellsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
for (int i = 1; i < a.length; i++) {
a[i] = (int)(Math.random()*100);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
ShellInsort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
public static void ShellInsort(int[] a){
int[] b = {4,2,1};
for (int i = 0; i < b.length; i++) {
Insert(a,b[i]);
}
}
public static void Insert(int[] a,int nrc){
int Inserpos = 0;
for (int i = 1+nrc; i < a.length; i++) {
if(a[i]<a[i-nrc]){
a[0]=a[i];
for (int k = i-nrc; k >= 0 ; k-=nrc) {
if(a[0]<a[k]){
a[k+nrc]=a[k];
if(k-nrc <= 0)
Inserpos=k;
}else{
Inserpos = k+nrc;
break; //因为前面是有序的 因此当检测到了 a[0]> = a[k]立即跳出循环!
}
}
a[Inserpos]=a[0];
a[0] = 0;
}
}
}
}