几个排序算法

冒泡排序

private static void bubble(int[] a,int length)
    {
        System.out.println("冒泡排序为:");
        int temp;
        boolean flag = true;
        for(int i=length;i>0;i--)
        {
            for(int j=1;j<i;j++)
            {
                if(a[j-1]>a[j])
                {
                    temp=a[j];
                    a[j]=a[j-1];
                    a[j-1]=temp;
                    flag=false;
                }
            }
            if(flag==true)
            {

                break;
            }
        }

    }

选择排序

 private static void select(int[] a,int length)
    {
        System.out.println("选择排序为:");
        int k,temp;
        for(int i=length;i>0;i--)
        {
            k=0;
            for(int j=0;j<i;j++)
            {
                if(a[j]>a[k])
                    k=j;
            }
            temp=a[k];
            a[k]=a[i-1];
            a[i-1]=temp;
        }
    }

插入排序

private static void Insert_sort(int[] a,int length)
    {
        System.out.println("插入排序为:");
        int temp,j;
        for(int i=1;i<length;i++) {
            temp=a[i];
            for (j =i-1; j>=0&&temp<a[j]; j--) {
                a[j+1]=a[j];
            }
            a[j+1]=temp;
        }
    }

快速排序

 private static void qucik_sort(int[] a,int left,int right)
    {
        if(left>=right)
            return;
        int i=left,j=right;
        int temp=a[i];
        while(i<j){
            while (a[j]>=temp&&i<j)
                j--;
            if(i<j) {
                a[i] = a[j];
                i++;
            }
            while(a[i]<=temp&&i<j)
                i++;
            if(i<j){
                a[j]=a[i];
                j--;
            }
        }
        a[i]=temp;
        qucik_sort(a,left,i-1);
        qucik_sort(a,i+1,right);

    }

堆排序

private static void heap_adjust(int[] a,int i,int length)
    {
        int max_parent=i;
        int lchild=i*2+1,rchild=i*2+2;
        if(lchild<length&&a[lchild]>a[max_parent])
            max_parent=lchild;
        if(rchild<length&&a[rchild]>a[max_parent])
            max_parent=rchild;
        if(max_parent!=i){
            int temp;
            temp=a[max_parent];
            a[max_parent]=a[i];
            a[i]=temp;
            heap_adjust(a,max_parent,length);

        }
    }
    private static void heap_sort(int[] a,int length)
    {
        System.out.println("堆排序结果为:");
        int temp;
//        建立初始堆
        for(int i=length/2-1;i>=0;i--){
            heap_adjust(a,i,length);
        }
        for(int i=length-1;i>=0;i--){
            temp=a[i];
            a[i]=a[0];
            a[0]=temp;
            heap_adjust(a,0,i);
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,以下是几种常见的 Java 版本排序算法的示例代码: 1. 冒泡排序: ``` public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 2. 选择排序: ``` public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } ``` 3. 插入排序: ``` public static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } ``` 4. 快速排序: ``` public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } private static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } ``` 请注意,以上代码仅供参考,您可以根据自己的需求对代 ### 回答2: 这里给你提供一些常见的Java版本的排序算法示例代码。 1. 冒泡排序(Bubble Sort)示例代码: ```java public void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { // 交换arr[j]和arr[j+1] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } ``` 2. 选择排序(Selection Sort)示例代码: ```java public void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // 交换arr[i]和arr[minIndex] int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } ``` 3. 插入排序(Insertion Sort)示例代码: ```java public void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } ``` 希望以上的示例代码能帮助你理解Java版本的几个排序算法。请注意,以上示例代码仅为参考,实际应用中可能需要对输入数据进行合理的边界处理和优化。 ### 回答3: 以下是Java版本的几个常见排序算法的示例代码: 1. 冒泡排序: ```java public class BubbleSort { public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (array[j] > array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } public static void main(String[] args) { int[] array = {64, 34, 25, 12, 22, 11, 90}; bubbleSort(array); System.out.println("冒泡排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 2. 选择排序: ```java public class SelectionSort { public static void selectionSort(int[] array) { int n = array.length; for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } int temp = array[minIndex]; array[minIndex] = array[i]; array[i] = temp; } } public static void main(String[] args) { int[] array = {64, 25, 12, 22, 11}; selectionSort(array); System.out.println("选择排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 3. 插入排序: ```java public class InsertionSort { public static void insertionSort(int[] array) { int n = array.length; for (int i = 1; i < n; ++i) { int key = array[i]; int j = i - 1; while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = key; } } public static void main(String[] args) { int[] array = {12, 11, 13, 5, 6}; insertionSort(array); System.out.println("插入排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 以上示例代码包含了冒泡排序、选择排序和插入排序的Java版本实现。你可以将要排序的数组传递给相应的排序函数,然后通过遍历数组打印出排序后的结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值