使用java操作数组排序

时间:2017-09-03 04:27:46
【文件属性】:

文件名称:使用java操作数组排序

文件大小:8KB

文件格式:JAVA

更新时间:2017-09-03 04:27:46

java 数组

package com.green.liu.sort; /* * AUTHOR:刘青青 * DATE:2012年8月10号 */ public class Sort { /* * 插入排序方法,对整数数组从小到大的排序 */ public void insertSortAsc(int arr[]) { for (int i = 1; i < arr.length; i++) { int insertVal = arr[i]; int index = i - 1; while (index >= 0 && insertVal < arr[index]) { arr[index + 1] = arr[index]; index--; } index++; arr[index] = insertVal; } } /* * 插入排序方法,对float数组从小到大的排序 */ public void insertSortAsc(float arr[]) { for (int m = 1; m < arr.length; m++) { float insertVal = arr[m]; int index = m - 1; for (; index >= 0; index--) { if (insertVal < arr[index]) { arr[index + 1] = arr[index]; arr[index] = insertVal; } } } } /* * 插入排序方法,对double数组从小到大的排序 */ public void insertSortAsc(double arr[]) { for (int m = 1; m < arr.length; m++) { double insertVal = arr[m]; int index = m - 1; for (; index >= 0; index--) { if (insertVal < arr[index]) { arr[index + 1] = arr[index]; arr[index] = insertVal; } } } } /* * 插入排序方法,对整数数组从大到小的排序 */ public void insetSortDesc(int arr[]) { for (int i = 1; i < arr.length; i++) { int insertVal = arr[i]; int index = i - 1; while (index >= 0 && insertVal > arr[index]) { arr[index + 1] = arr[index]; index--; } index++; arr[index] = insertVal; } } /* * 插入排序方法,对float数组从大到小的排序 */ public void insetSortDesc(float arr[]) { for (int m = 1; m < arr.length; m++) { float insertVal = arr[m]; int index = m - 1; for (; index >= 0; index--) { if (insertVal > arr[index]) { arr[index + 1] = arr[index]; arr[index] = insertVal; } } } } /* * 插入排序方法,对float数组从大到小的排序 */ public void insetSortDesc(double arr[]) { for (int m = 1; m < arr.length; m++) { double insertVal = arr[m]; int index = m - 1; for (; index >= 0; index--) { if (insertVal > arr[index]) { arr[index + 1] = arr[index]; arr[index] = insertVal; } } } } /* * 冒泡排序方法,对int数组从小到大的排序 */ public void bubbleSortAsc(int arr[]) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * * 冒泡排序方法,对int数组从大到小的排序 */ public void bubbleSortDesc(int arr[]) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * 冒泡排序方法,对float数组从小到大的排序 */ public void bubbleSortAsc(float arr[]) { float temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * * 冒泡排序方法,对float数组从大到小的排序 */ public void bubbleSortDesc(float arr[]) { float temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * 冒泡排序方法,对double数组从小到大的排序 */ public void bubbleSortAsc(double arr[]) { double temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * * 冒泡排序方法,对double数组从大到小的排序 */ public void bubbleSortDesc(double arr[]) { double temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /* * 快速排序方法,对int数组从小到大的排序 */ public void quickSortAsc(int left, int right, int arr[]) { int l = left; int r = right; int temp = 0; int pivot = arr[(left + right) / 2]; while (l < r) { while (arr[l] < pivot) l++; while (arr[r] < pivot) r++; if (l >= r) break; temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; if (arr[l] == pivot) --r; if (arr[r] == pivot) ++l; } } /* * 快速排序方法,对int数组从大到小的排序 */ public void quickSortDesc(int arr[]) { } /* * 选择排序,对int数组由小到大 */ public void selectSortAsc(int arr[]) { int tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { int min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } /* * 选择排序,对int数组由大到小 */ public void selectSortDesc(int arr[]) { int tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { int min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min < arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } /* * 选择排序,对float数组由小到大 */ public void selectSortAsc(float arr[]) { float tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { float min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } /* * 选择排序,对float数组由大到小 */ public void selectSortDesc(float arr[]) { float tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { float min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min < arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } /* * 选择排序,对float数组由小到大 */ public void selectSortAsc(double arr[]) { double tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { double min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } /* * 选择排序,对float数组由大到小 */ public void selectSortDesc(double arr[]) { double tempValue = 0; for (int i = 0; i < arr.length - 1; i++) { double min = arr[i]; int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (min < arr[j]) { min = arr[j]; minIndex = j; } } tempValue = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tempValue; } } public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = { 3, 68, -1, 0, 100, 90, 78, 787, 899, 90 }; Sort sort = new Sort(); sort.selectSortAsc(arr); for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] + " "); } } }


网友评论