主要排序算法Java实现

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  4,快速排序

  import java.util.Random;

  public class QuickSortImpl {

  /**

  * @param args

  */

  public static void swap(int a, int b) {

  int temp = a;

  a = b;

  b = temp;

  }

  public static void QuickSort(int A[], int left, int right) {

  if (left < right) {

  int i = left;

  int j = right;

  int pivot = A[left];

  while(left<right){

  while(A[left]<pivot) {

  ++left;

  }

  while(A[right]>pivot){

  –right;

  }

  if(left<right){

  int temp=A[left];

  A[left]=A[right];

  A[right]=temp;

  }

  }

  int temp=A[right];

  A[right]=pivot;

  pivot=temp;

  QuickSort(A, i, right- 1);

  QuickSort(A, right+1, j);

  }

  }

  public static void main(String[] args) {

  // TODO Auto-generated method stub

  int A[]=new int[]{3,7,2,35,8,9};

  QuickSort(A,0,5);

  for(int i=0;i<5;i++){

  System.out.println(A[i]);

  }

  }

  }

  5,选择排序

  public class SortedSortImpl {

  public static void SelectSort(int A[]) {// 通过n-i此关键字比较,从

  // n-i+1个记录中选出最小的与第i个记录交换

  int n = A.length;

  for (int i = 0; i < n; i++) {

  int k = i;

  for (int j = i + 1; j < n; j++) {

  if (A[k] > A[j]) {

  k = j;

  }

  }

  int temp = A[i];

  A[i] = A[k];

  A[k] = temp;

  }

  }

  /**

  * @param args

  */

  public static void main(String[] args) {

  // TODO Auto-generated method stub

  int A[] = new int[] { 2, 5, 3, 9, 7, 1, 30 };

  SelectSort(A);

  for (int i = 0; i < A.length; i++) {

  System.out.println(A[i]);

  }

  }

  }

[1][2]

不给自己一点轻松的机会,好象世界的每个角落都需要自己的脚去留个痕迹,才叫人生。

主要排序算法Java实现

相关文章:

你感兴趣的文章:

标签云: