• 数组排序----Demo


    //选择排序,分为简单选择排序、树形选择排序(锦标赛排序)、堆排序 此算法为简单选择排序
    public static void selectSort(int[] a){
    	for(int i=0;i<a.length;i++){
    		 int minIndex = i;
             for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
             }
             if (minIndex != i) {
                int temp = a[minIndex];
                a[minIndex] = a[i];
                a[i] = temp;
             }
    	}
    }
    

      

    package com.test; public class Sort { 
    //打印数组
    public static void displayData(int[] data){
    for(int d:data){ System.out.println(d+""); } System.out.println(); }
    //冒泡排序算法,时间复杂度O(n2),算法具有稳定性,堆排序和快速排序算法不具有稳定性,即排序后相同元素的顺序会发生变化
    public static void bubbleSort(int[] src){ if(src.length>0){ int length=src.length; for(int i=1;i<length;i++){ for(int j=0;j<length-i;j++){ if(src[j]>src[j+1]){ int temp=src[j]; src[j]=src[j+1]; src[j+1]=temp; } } } } }
    //快速排序,时间复杂度O(nlogn),最坏时间复杂度O(n2),平均时间复杂度O(nlogn),算法不具稳定性
    public static void quickSort(int[] src,int begin,int end){ if(begin<end){ int key=src[begin]; int i=begin; int j=end; while(i<j){ while(i<j && src[j]>key){ j--; }if(i<j){ src[i]=src[j]; i++; }while(i<j && src[i]<key){ i++; }if(i<j){ src[j]=src[i]; j--; } } src[i]=src[key]; quickSort(src, begin, i-1); quickSort(src, i+1, end); } } }

      

    package com.test; import java.util.Arrays; public class Test { public static void compare(int []a) { for(int i=0;i<a.length-1;i++)
    //外层循环控制比较轮数,从a[0]到a[a.length-2],最后一个数不用比较 { for(int j=i+1;j<a.length;j++)
    //内层循环是要比较的数,总是在a[i]的后面,所以从i+1的位置开始,一直到最后一个数 { if(a[i]>a[j])
    //如果大于进行交换 { int t=a[i]; a[i]=a[j]; a[j]=t; } } } } public static void main(String[] args) { int[] a={3,6,2,1,88,44,99,4,32}; compare(a); System.out.println(Arrays.toString(a)); } }

      

    package com.test;
    
    import java.util.Arrays;
    
    public class MergeSort {
    	//归并排序,将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列 
    	public static int[] sort(int[]nums,int low,int high){
    		int mid=(low+high)/2;
    		if(low<high){
    			sort(nums,low,mid);//左边
    			sort(nums,mid+1,high);//右边
    			merge(nums,low,mid,high);//左右合并
    		}
    		return nums;
    	}
    	public static void merge(int[] nums,int low,int mid,int high){
    		int[] temp=new int[high-low+1];
    		int i=low;//左指针
    		int j=mid+1;//右指针
    		int k=0;
    		// 把较小的数先移到新数组中
    		while(i<=mid && j<=high){
    			if(nums[i]<nums[j]){
    				temp[k++]=nums[i++];
    			}else{
    				temp[k++]=nums[j++];
    			}
    		}
    		// 把左边剩余的数移入数组
    		while(i<=mid){
    			temp[k++]=nums[i++];
    		}
    		// 把右边剩余的数移入数组
    		while(j<=high){
    		temp[k++]=nums[j++];
    		}
    		// 把新数组中的数覆盖nums数组
    		for(int k2=0;k2<temp.length;k2++){
    			nums[k2+low]=temp[k2];
    		}
    	}
    	public static void main(String[] args) {
    		int[] nums={45,98,9,3,6};
    		MergeSort.sort(nums, 0, nums.length-1);
    		System.out.println(Arrays.toString(nums));
    	}
    }
    

      

  • 相关阅读:
    【收藏】如何理解二维数组
    【algo&ds】9.拓扑排序、AOV&AOE、关键路径问题
    【algo&ds】8.最小生成树
    【algo&ds】7.最短路径问题
    【algo&ds】6.图及其存储结构、遍历
    【c&c++】变量初始化
    【algo&ds】【pat】5.并查集及其应用
    【algo&ds】4.B树、字典树、红黑树、跳表
    【algo&ds】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、散列表
    【algo&ds】3.栈和队列
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6420848.html
Copyright © 2020-2023  润新知