java--数据结构--8种排序算法

1.直接插入排序
直接插入排序:
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    insertionSort(a);
    for(int i = 0;i < a.length;i++)
      System.out.print(a[i] + " ");
  }
// insertionSort必须为static方法,因为不能再静态上下文中引用非静态的方法
  public static int[] insertionSort(int a[]) {
    for(int i = 1;i < a.length;i++) {
      for(int j = i;j > 0;j--) {
        if(a[j] < a[j-1]) {
          int temp = a[j];
          a[j] = a[j-1];
          a[j-1] = temp;
        }
      }
    }
    return a;
  }
}
2.折半插入排序
折半插入排序:
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    binaryInsertSort(a);
    for(int i = 0;i< a.length;i++){
      System.out.println(a[i] + "");
    }
  }

  public static int[] binaryInsertSort(int a[]) {
    for(int i = 1;i < a.length;i++) {
      int temp = a[i];
      int low = 0;
      int high = i-1;

      while(low <= high) {
        int mid = (low + high)/2;
       if(temp < a[mid]) {
         high = mid - 1;
       }else{
         low = mid + 1;
       }
      }

      for(int j = i; j >=low + 1;j--){
        a[j] = a[j-1];
      }
      a[low] = temp;
    }
    return a;
  }
}
3.希尔排序

希尔排序
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    shellInsertSort(a);
    for(int i = 0;i< a.length;i++){
      System.out.println(a[i] + "");
    }
  }

  public static int[] shellInsertSort(int a[]) {
    int d = a.length;
    while(true)
    {
      d = d/2;
      for(int x = 0;x < d;x++){
        for(int i = x + d;i < a.length;i = i + d) {
          int temp = a[i];
          int j;
          // 注意判断条件j >= 0要在a[j] > temp前边,否则若j为负,a[j] > temp中数组不合法会运行报错
          for(j = i - d; j >= 0 && a[j] > temp;j = j-d) {
            a[j+d] = a[j];
          }
          a[j+d] = temp;
        }
      }
      if(d == 1){
        break;
      }
    }
    return a;
  }
}
4.简单选择排序
简单选择排序
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    selectSort(a);
    for(int i = 0;i< a.length;i++){
      System.out.println(a[i] + "");
    }
  }

  public static int[] selectSort(int a[]) {
    int i,j,minIndex;
    for(i = 0;i < a.length;i++){
      minIndex = i;
      for(j = i + 1;j < a.length;j++){
        if(a[minIndex] > a[j]){
          minIndex = j;
        }
      }
      int temp = a[i];
      a[i] = a[minIndex];
      a[minIndex] = temp;
    }
    return a;
  }
}
5.堆排序

堆排序:(答案还有问题)
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    heapSort(a);
    for(int i = 0;i< a.length;i++){
      System.out.print(a[i] + " ");
    }
  }

  public static int[] heapSort(int a[]) {

    for(int i = a.length/2 - 1;i >= 0;--i){
      heapAdjust(a,i);
    }

    for(int i = a.length - 1;i > 0;--i) {
      int temp = a[i];
      a[i] = a[0];
      a[0] = temp;
      heapAdjust(a,i);
    }
    return a;
  }

  public static int[] heapAdjust(int a[],int i) {
    int temp = a[i];
    int child = 2*i + 1;
    while(child < a.length){

      if(child + 1 < a.length && a[child] < a[child+1]) {
        ++child;
      }
      if(a[i] < a[child]) {
        a[i] = a[child];
        i = child;
        child = 2 * i + 1;
      }else {
        break;
      }
    }
    a[i] = temp;
    return a;
  }
}



public class HeapSortTest {

   public static void main(String[] args) {
     int[] data5 = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
       print(data5);
      heapSort(data5);
       System.out.println("排序后的数组:");
     print(data5);
   }

   public static void swap(int[] data, int i, int j) {
       if (i == j) {
          return;
       }
       data[i] = data[i] + data[j];
        data[j] = data[i] - data[j];
        data[i] = data[i] - data[j];
   }

    public static void heapSort(int[] data) {
        for (int i = 0; i < data.length; i++) {
            createMaxdHeap(data, data.length - 1 - i);
            swap(data, 0, data.length - 1 - i);
            print(data);
        }
    }

    public static void createMaxdHeap(int[] data, int lastIndex) {
       for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
            // 保存当前正在判断的节点
            int k = i;
            // 若当前节点的子节点存在
            while (2 * k + 1 <= lastIndex) {
                // biggerIndex总是记录较大节点的值,先赋值为当前判断节点的左子节点
               int biggerIndex = 2 * k + 1;
                if (biggerIndex < lastIndex) {
                   // 若右子节点存在,否则此时biggerIndex应该等于 lastIndex
                   if (data[biggerIndex] < data[biggerIndex + 1]) {
                        // 若右子节点值比左子节点值大,则biggerIndex记录的是右子节点的值
                        biggerIndex++;
                   }
              }
                if (data[k] < data[biggerIndex]) {
                    // 若当前节点值比子节点最大值小,则交换2者得值,交换后将biggerIndex值赋值给k
                   swap(data, k, biggerIndex);
                   k = biggerIndex;
                } else {
                    break;
                }
            }
        }
   }

    public static void print(int[] data) {
       for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + "\t");
       }
        System.out.println();
    }

}
6.冒泡排序
冒泡排序:
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    bubbleSort(a);
    for(int i = 0;i< a.length;i++){
      System.out.print(a[i] + " ");
    }
  }

  public static int[] bubbleSort(int a[]) {
    int i = a.length;
    int temp,j;
    while(i > 0) {
      for(j = 0;j < i-1;j++) {
        if(a[j] > a[j+1]) {
          temp = a[j];
          a[j] = a[j+1];
          a[j+1] = temp;
        }
      }
      i--;
    }
    return a;
  }
}
7.快速排序
快速排序:
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    quickSort(a,0,a.length-1);
    for(int i = 0;i< a.length;i++){
      System.out.print(a[i] + " ");
    }
  }

  public static int[] quickSort(int a[],int left,int right) {
    int dp;
    if(left < right) {
      dp = partition(a,left,right);
      quickSort(a,left,dp - 1);
      quickSort(a,dp + 1,right);
    }
    return a;
  }

  public static int partition(int a[], int left,int right) {

    int temp;
    int pivot = a[left];
    while(left < right) {
      while(left < right && a[right] > pivot) {
        right--;
      }
      if(left < right) {
        temp = a[left];
        a[left] = a[right];
        a[right] = temp;
      }
      while(left < right && a[left] < pivot) {
        left++;
      }
      if(left < right) {
        temp = a[left];
        a[left] = a[right];
        a[right] = temp;
      }
      a[left] = pivot;

    }
    return left;
  }
}
8.归并排序
归并排序:
public class SortArray {
  public static void main(String[] args) {
    int a[] = {2,4,6,1,3,8,9,7,5};
    mergeSort(a,0,a.length-1);
    for(int i = 0;i < a.length;i++){
      System.out.print(a[i] + " ");
    }
  }

  public static int[] mergeSort(int a[],int low,int high) {
    int mid = (low + high)/2;
    if(low < high) {
      mergeSort(a,low,mid);
      mergeSort(a,mid+1,high);
      merge(a,low,mid,high);
    }
    return a;
  }

  public static void merge(int a[],int low,int mid,int high) {
    int array[] = new int[high - low +1];
    //左指针
    int i = low;
    // 右指针
    int j = mid + 1;
    int k = 0;
    // 把较小的数先移到新数组中
    while(i <= mid && j <= high) {
      if(a[i] < a[j]) {
        array[k++] = a[i++];
      }else {
        array[k++] = a[j++];
      }
    }
    // 把左边剩余的数移到数组
    while(i <= mid) {
      array[k++] = a[i++];
    }
    // 把右边剩余的数移到数组
    while(j <= high) {
      array[k++] = a[j++];
    }
    // 把新数组中的数覆盖a数组
    for(int k2 = 0;k2 <array.length;k2++) {
      a[k2 + low] = array[k2];
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了多数据结构算法排序的实现。以下是一些常见的排序算法: 1. 冒泡排序(Bubble Sort):通过不断比较相邻元素并交换,将最大(或最小)的元素逐渐“冒泡”到最后(或最前)位置。时间复杂度为O(n^2)。 2. 选择排序(Selection Sort):每次选择未排序部分中最小(或最大)的元素,放到已排序部分的末尾(或开头)。时间复杂度为O(n^2)。 3. 插入排序(Insertion Sort):将未排序部分的元素逐个插入已排序部分的正确位置。时间复杂度为O(n^2)。 4. 快速排序(Quick Sort):通过选择一个基准元素,将列表分为两个子列表,其中一个子列表的所有元素都小于(或大于)基准元素,然后递归地排序两个子列表。时间复杂度平均情况为O(n log n)。 5. 归并排序(Merge Sort):将列表拆分为多个子列表,递归地对每个子列表进行排序,然后合并这些有序子列表以获得最终有序列表。时间复杂度为O(n log n)。 6. 堆排序(Heap Sort):将待排序元素构建成一个最大(或最小)堆,然后逐个移除堆顶元素并将其放入已排序部分。时间复杂度为O(n log n)。 7. 希尔排序(Shell Sort):将列表按照一定的间隔分组,并对每个分组进行插入排序,然后逐渐缩小间隔,直到间隔为1,最后再进行一次插入排序。时间复杂度取决于间隔序列的选择。 Java中提供了Arrays类和Collections类来对数组和集合进行排序,可以使用它们的sort方法。另外,你也可以自己实现这些排序算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值