java 常用算法

求出1000之内的所有水仙花数

水仙花数指的是一个三位整数,它的各位数字的立方和等于这个数本身.。

public static void main(String[] args) {  
        // TODO Auto-generated method stub  
         int hundred, ten, bits;    
         System.out.println("水仙花数为:");    
         for (int i = 100; i <= 999; i++)    
         {    
             hundred = i / 100;    
             ten = i % 100 / 10;    
             bits = i % 10;    
             if (i == hundred * hundred * hundred + ten * ten * ten + bits * bits * bits)    
             {    
                 System.out.print(i + "    ");    
             }    
         }      
    }  

几种常用的排序算法

冒泡排序

在这里插入图片描述

每一趟只能确定将一个数归位。即第一趟只能确定将末位上的数归位,第二趟只能将倒数第 2 位上的数归位,依次类推下去。如果有 n 个数进行排序,只需将 n-1 个数归位,也就是要进行 n-1 趟操作。
而 “每一趟 ” 都需要从第一位开始进行相邻的两个数的比较,将较大的数放后面,比较完毕之后向后挪一位继续比较下面两个相邻的两个数大小关系,重复此步骤,直到最后一个还没归位的数。

//冒泡排序两两比较的元素是没有被排序过的元素--->
public void bubbleSort(int[] array){
    for(int i=0;i<array.length-1;i++){//控制比较轮次,一共 n-1 趟
        for(int j=0;j<array.length-1-i;j++){//控制两个挨着的元素进行比较
            if(array[j] > array[j+1]){
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}
//优化版本
public static int[] bubbleSort(int[] arr) {
     if (arr == null || arr.length < 2) {
          return arr;
     }
    for (int i = 0; i < arr.length - 1; i++) {
         boolean isSorted  = true;//有序标记,每一轮的初始是true
         for (int j = 0; j < arr.length -i - 1; j++) {
             if (arr[j + 1] < arr[j]) {
                 isSorted  = false;//有元素交换,所以不是有序,标记变为false
                 int t = arr[j];
                 arr[j] = arr[j+1];
                 arr[j+1] = t;
             }
         }
         //一趟下来是否发生位置交换,如果没有交换直接跳出大循环
         if(isSorted )
              break;
     }
     return arr;
}
//优化升级版本
public static int[] bubbleSort(int[] arr) {
     if (arr == null || arr.length < 2) {
          return arr;
     }
    //记录最后一次交换的位置
    int lastExchangeIndex = 0;
    //无序数列的边界,每次比较只需要比到这里为止
    int sortBorder = arr.length - 1;
    
    for (int i = 0; i < arr.length - 1; i++) {
         boolean isSorted  = true;//有序标记,每一轮的初始是true
         for (int j = 0; j < sortBorder; j++) {
             if (arr[j + 1] < arr[j]) {
                 isSorted  = false;//有元素交换,所以不是有序,标记变为false
                 int t = arr[j];
                 arr[j] = arr[j+1];
                 arr[j+1] = t;
                 lastExchangeIndex = j;
             }
         }
        sortBorder = lastExchangeIndex
         //一趟下来是否发生位置交换,如果没有交换直接跳出大循环
         if(isSorted )
              break;
     }
     return arr;
}

选择排序

public class SelectionSort {
    public void selectionSort(int[] array) {
       int temp;
       for (int i = 0; i < array.length - 1; i++) {
              for (int j = i + 1; j <= array.length - 1; j++) {
              if (array[i] > array[j]) {    
              // 注意和冒泡排序的区别,这里是i和j比较。
              temp = array[i];
              array[i] = array[j];
              array[j] = temp;
             }
        }            // 打印每趟排序结果
       for (int m = 0; m <= array.length - 1; m++) {
             System.out.print(array[m] + "\t");
       }
          System.out.println();
       }
    }    
    public static void main(String[] args) {
        SelectionSort selectionSort = new SelectionSort(); 
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        selectionSort.selectionSort(array);        
    for (int m = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}

插入排序

public class InsertSort {
    public void insertSort(int[] array, int first, int last) {        
    int temp, i, j;        
    for (i = first + 1; i <= last - 1; i++) {
            temp = array[i];
            j = i - 1;            
        while (j >= first && array[j] > temp) {
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = temp;            // 打印每次排序结果
            for (int m = 0; m <= array.length - 1; m++) {
                System.out.print(array[m] + "\t");
            }
            System.out.println();
        }
    }    
     public static void main(String[] args) {
        InsertSort insertSort = new InsertSort();
            int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        insertSort.insertSort(array, 0, array.length);      
             for (int i = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
    }
}

快速排序

public class QuickSort {    
    public int partition(int[] sortArray, int low, int height) {        
    int key = sortArray[low];        
    while (low < height) {            
        while (low < height && sortArray[height] >= key)
            height--;
            sortArray[low] = sortArray[height];            
            while (low < height && sortArray[low] <= key)
                low++;
                sortArray[height] = sortArray[low];
            }
            sortArray[low] = key;        // 打印每次排序结果
        for (int i = 0; i <= sortArray.length - 1; i++) {
            System.out.print(sortArray[i] + "\t");
        }
        System.out.println();        
        return low;
    }    
    public void sort(int[] sortArray, int low, int height) {        
        if (low < height) {            
            int result = partition(sortArray, low, height);
            sort(sortArray, low, result - 1);
            sort(sortArray, result + 1, height);
        }
    }
    public static void main(String[] args) {
        QuickSort quickSort = new QuickSort();        
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };        
        for (int i = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
        quickSort.sort(array, 0, 8);        
        for (int i = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
    }
}

希尔排序

public class ShellSort {
    public void shellSort(int[] array, int n) {
        int i, j, gap;
        int temp;
        for (gap = n / 2; gap > 0; gap /= 2) {
            for (i = gap; i < n; i++) {       
                for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j -= gap) {
                    temp = array[j];
                    array[j] = array[j + gap];
                    array[j + gap] = temp;
                }                // 打印每趟排序结果
                for (int m = 0; m <= array.length - 1; m++) {
                    System.out.print(array[m] + "\t");
                }
                System.out.println();
            }
        }
    } 
     public static void main(String[] args) {
        ShellSort shellSort = new ShellSort();
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        shellSort.shellSort(array, array.length);        
        for (int m = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}

几种排序算法的比较

1.稳定性比较

​ 插入排序、冒泡排序、二叉树排序、二路归并排序及其他线形排序是稳定的

​ 选择排序、希尔排序、快速排序、堆排序是不稳定的

2.时间复杂性比较

​ 插入排序、冒泡排序、选择排序的时间复杂性为O(n2)

​ 其它非线形排序的时间复杂性为O(nlog2n)

​ 线形排序的时间复杂性为O(n);

3.辅助空间的比较

​ 线形排序、二路归并排序的辅助空间为O(n);

​ 其它排序的辅助空间为O(1);

4.其它比较

​ 插入、冒泡排序的速度较慢,但参加排序的序列局部或整体有序时,这种排序能达到较快的速度,但是在这种情况下,快速排序反而慢了。

​ 当n较小时,对稳定性不作要求时宜用选择排序,对稳定性有要求时宜用插入或冒泡排序。

​ 若待排序的记录的关键字在一个明显有限范围内时,且空间允许是用桶排序。

​ 当n较大时,关键字元素比较随机,对稳定性没要求宜用快速排序。

​ 当n较大时,关键字元素可能出现本身是有序的,对稳定性有要求时,空间允许的情况下宜用归并排序。

​ 当n较大时,关键字元素可能出现本身是有序的,对稳定性没有要求时宜用堆排序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值