1.冒泡输入法
原 1 3 2 5 1 0
1 2 3 1 0 5
1 2 1 0 3 5
1 1 0 2 3 5
1 0 1 2 3 5
终 0 1 1 2 3 5
相邻两个数作比较 ,如果后边一个数大于前边一个数 ,这两个数交换
public static void maopao(int arr[]){
for(int i=0;i<arr.length-1; i++){
for(int j=i;j<arr.length-2;j++){
if(arr[j]>arr[j+1]){
int temp =arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("maopao");
for(int k:arr){
System.out.print(k+" ");
}
}
2.选择排序
原 1 3 2 5 1 0
0 3 2 5 1 1
0 1 3 5 2 1
0 1 1 5 3 2
0 1 1 2 5 3
最终 0 1 1 2 3 5
排完序以后
public static void sort(int arr[]){
int i,j,min;
for(i=0;i<arr.length-1;i++){
min =i;
for(j=i+1;j<arr.length;j++){
if(arr[min]>arr[j]){
min=j;
}
}
if(i!=min){
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
for(int k:arr){
System.out.print(k+" ");
}
}
3.插入排序
拿出一个数从后往前比较,如果小于和它比的那个数就交换,前面有几个数就判断几次(即第二个循环)
1 3 2 5 1 0
先拿出第一个数
1 3 2 5 1 0
拿第二个数 3 不小于 1 ,进入else 不做任何操作(因为它比它前面一个数大,它就比前面所有的数都大)直接跳出循环
1 3 2 5 1 0
拿第三个数2先跟3比,小于3,交换, 就是 1 2 3 , 然后2跟1比 不小于1 不交换
1 3 2 5 1 0 ----> 1 2 3 5 1 0
拿第四个数5 和3比较 5不小于3,进入else 不做任何操作(因为它比它前面一个数大,它就比前面所有的数都大)直接跳出循环
1 2 3 5 1 0
拿第5个数1,1和5比较,1小于5 ,交换 1 2 3 1 5 0 然后 1和3比 小于3 交换 1 2 1 3 5 0,1 和 2比较 1小于2 交换 1 1 2 3 5 0 ,1和1比较1不小于不满足条件,进入else做任何操作(因为它比它前面一个数大,它就比前面所有的数都大)直接跳出循环,
1 1 2 3 5 0
拿最后一个数 0 和 5比较 小于5 交换 1 1 2 3 0 5 ,然后0 和3比较,小于3交换 1 1 2 0 3 5,0和 2比较 小于2 交换
1 1 0 2 3 5 0,0和1比较 小于1 交换 1 0 1 2 3 5,然后0和1比较,0小于1 交换 0 1 1 2 3 5
0 1 1 2 3 5
完成
public static void sort1(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j > 0; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}else{
break;
}
}
}
for(int m:a){
System.out.print(m+" ");
}
System.out.println();
}
4.希尔排序
假设待排序文件有10个记录,其关键字分别是:
49,38,65,97,76,13,27,49,55,04。
增量序列的取值依次为:
5,2,1
public static void xier(int arr[]){
for(int i=ifou(arr.length)/2;i>=1;i=ifou(i)/2){ //求增量及根据增量判断结束条件
for(int j=0;j<arr.length-1;j++){ //从数组中取数
for(int k=j;k+i<arr.length;k+=i){ //每一个都加上增量 跟他们那一组数进行过交换
if(arr[k]>arr[k+i]){
int temp =arr[k];
arr[k]=arr[k+i];
arr[k+i]=temp;
}
}
}
//如果 i=1 就结束循环否则会无限循环
if(i==1){
break;
}
}
for(int num:arr){
System.out.print(num+" ");
}
}
5.基数排序
public class Jishu {
public static void main(String[] args) {
int []a ={250,340,520,25,7,63,55,49,0};
//获取数组中最大的数
int max =Arrays.stream(a).max().getAsInt();
//获取最大数有多少位
int i=(max +"").length();
for(int n=1;n<=i;n++){
//创建一个列数为10的list集合
List<Integer>[] list = new ArrayList[10];
//初始化每一个list集合
for(int m=0;m<10;m++){
list[m] = new ArrayList<>();
}
//将数组中个位相同的放一个 集合中,相当于一次排序,然后在对高位进行排序
for(int num:a){
int bit =getBit(num,n);
list[bit].add(num);
}
//每次针对一位进行排序后,读取到一个数组中,然后接着对高位排序
list2array(list,a);
}
}
//将针对某一位排好序后放在list中
private static void list2array(List<Integer>[] list, int[] a) {
int i=0;
for(List<Integer> integers:list){
for(int m=0;m < integers.size();m++,i++){
a[i] = integers.get(m);
}
}
//将数组中的数输出
System.out.println(Arrays.toString(a));
}
/**
*
* @param num 某个数据
* @param bit 第几位
* @return 某一位的值
*/
public static int getBit(int num,int bit){
int chushu=(int) Math.pow(10,bit-1);
return num/chushu%10;
}
}
6.归并排序
7.快速排序
public class quick_sort {
public static void main(String[] args) {
int[] A = { 3, 2, 1,3,5,4,5,3,0};
//将array转换为arraylist
List<Integer> list=new ArrayList<Integer>();
for (int i = 0; i < A.length; i++) {
list.add(A[i]);
}
//调用函数得到结果
list=quickSort(list);
//输出结果
for (Integer integer : list) {
System.out.print(integer+" ");
}
}
public static List<Integer> quickSort(List<Integer> num){
if (num.size()<=1) {
return num;
}
List<Integer> left = new ArrayList<Integer>();
List<Integer> right = new ArrayList<Integer>();
int key = num.get(0);
for(int i=1;i<num.size();i++){
if(num.get(i) < key){
left.add(num.get(i));
}
if(num.get(i)>= key){
right.add(num.get(i));
}
}
left = quickSort(left);
System.out.println("1----");
for(int m:left){
System.out.print(m);
}
System.out.println();
right = quickSort(right);
System.out.println("2----");
for(int m:right){
System.out.print(m);
}
System.out.println();
left.add(key);
System.out.println("3----");
for(int m:left){
System.out.print(m);
}
System.out.println();
left.addAll(right);
System.out.println("4----");
for(int m:left){
System.out.print(m);
}
System.out.println();
return left;
}
}
8.堆排序
如图所示
public class heap{
public static void creatTree(int a[]){
//创建一个队列
LinkedList<treeNode> listNode = new LinkedList();
treeNode root = new treeNode(a[0]);
listNode.addFirst(root);
int temp;
for(int i=1;i<a.length;i++) {
treeNode first = listNode.getFirst();
if (first.getLeft() == null) {
treeNode newNode = new treeNode(a[i]);
//将新节点放入队列
listNode.add(newNode);
//将新节点插入完全二叉树中,并作为子节点插入到某个父节点下
first.setLeft(newNode);
newNode.setParent(first);
//如果子节点的值大于父节点 ,进行交换
while (newNode.value>first.value){
temp=newNode.value;
newNode.value=first.value;
first.value=temp;
//如果父节点不是根节点
if(first==root){
break;
}
//将父节点当作新节点 与父节点的父节点进行加比较
newNode = first;
first=first.getParent();
}
}
else if(first.getRight()==null){
treeNode newNode = new treeNode(a[i]);
//将新节点放入队列
listNode.add(newNode);
//将新节点插入完全二叉树中 ,并作为子节点插入到某个父节点下
first.setRight(newNode);
newNode.setParent(first);
//将有两个子节点的父节点从队列中移除,保证知道下一次子节点插在那个父节点下
listNode.removeFirst();
//如果子节点的值大于父节点 ,进行交换
while (newNode.value>first.value){
temp=newNode.value;
newNode.value=first.value;
first.value=temp;
//如果父节点不是根节点
if(first==root){
break;
}
//将父节点当作新节点 与父节点的父节点进行加比较
newNode = first;
first=first.getParent();
}
}
}
//新建队列
LinkedList<treeNode> queue = new LinkedList();
int flag = 0;
queue.add(root);
treeNode rroot ;
//广度优先便利完全二叉树,将之填充到队列中去
while(queue.size()!=a.length){
rroot = queue.get(flag++);
if(rroot.getLeft() != null){
queue.addLast(rroot.getLeft());
}
if(rroot.getRight() !=null){
queue.addLast(rroot.getRight());
}
}
for(int i = 0;i<queue.size();i++){
System.out.print(queue.get(i).value+" ");
}
}
public static void main(String[] args) {
int a[] = {98,97,50,100,120,40,35};
creatTree(a);
}
}
public class treeNode {
private treeNode left;
private treeNode right;
private treeNode parent;
int value;
public treeNode getLeft() {
return left;
}
public void setLeft(treeNode left) {
this.left = left;
}
public treeNode getRight() {
return right;
}
public void setRight(treeNode right) {
this.right = right;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public treeNode getParent() {
return parent;
}
public void setParent(treeNode parent) {
this.parent = parent;
}
public treeNode(int value){
this.value = value;
}
public treeNode(){
}
}
public class heap {
//对最后一个数递归排序
public static void heapify (int arr[],int currentRootNode,int size){
int max = currentRootNode;
if(currentRootNode<size){
int left = 2* currentRootNode+1;
int right = 2* currentRootNode +2;
if(left < size){
if(arr[left] > arr[max]){
max = left;
}
}
if(right < size){
if(arr[right] > arr[max]){
max = right;
}
}
if(max!=currentRootNode){
int temp =arr[currentRootNode];
arr[currentRootNode]=arr[max];
arr[max]= temp;
heapify(arr, max, size);
}
}
}
public static void maxHeapify(int arr[],int size){
for(int i=size-1;i>=0;i--){
heapify(arr, i, size);
}
}
public static void main(String[] args) {
//目标小顶堆
int arrays[]={1,2,5,3,8,0,10};
for (int i = 0; i < arrays.length; i++) {
//每次建堆就可以排除一个元素了
maxHeapify(arrays, arrays.length - i);
//交换
int temp = arrays[0];
arrays[0] = arrays[(arrays.length - 1) - i];
arrays[(arrays.length - 1) - i] = temp;
}
for(int m:arrays){
System.out.print(m+" ");
}
}
}