赞
踩
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- boolean flag = false;
- Date date = new Date();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateStr = simpleDateFormat.format(date);
- System.out.println(dateStr);
- int[] arr = { 3, -1, 10, 9, 20 };
- 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]) {
- flag = true;
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- if (!flag) {
- break;
- } else {
- flag = false;
- }
- }
- System.out.println(Arrays.toString(arr));
- Date date1 = new Date();
- SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateStr1 = simpleDateFormat1.format(date);
- System.out.println(dateStr1);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = { 3, -1, 10, 9, -2 };
- for (int i = 0; i < arr.length - 1; i++) {
- int min = arr[i];
- int index = i;
- for (int j = i + 1; j < arr.length; j++) {
- if (min > arr[j]) {
- min = arr[j];
- index = j;
- }
- }
- if (index != i) {
- arr[index] = arr[i];
- arr[i] = min;
- }
- }
- System.out.println(Arrays.toString(arr));
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = { 3, -1, 10, 9, 2 };
- for (int i = 1; i < arr.length; i++) {
- int insertVal = arr[i];
- int insertIndex = i - 1;
- while (insertIndex >= 0 && arr[insertIndex] > insertVal) {
- arr[insertIndex + 1] = arr[insertIndex];
- insertIndex--;
- }
- arr[insertIndex + 1] = insertVal;
- }
- System.out.println(Arrays.toString(arr));
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = { 3, -1, 10, 9, 20 };
- for (int i = arr.length / 2; i > 0; i /= 2) {
- for (int j = i; j < arr.length; j++) {
- for (int k = j - i; k >= 0; k -= i) {
- if (arr[k] > arr[k + i]) {
- int temp = arr[k];
- arr[k] = arr[k + i];
- arr[k + i] = temp;
- }
- }
- }
- }
- System.out.println(Arrays.toString(arr));
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = { 3, -1, 10, 9, 20 };
- int[] temp = new int[arr.length];
- mergesort(arr, 0, arr.length-1, temp);
- System.out.println(Arrays.toString(temp));
- }
-
- public static void mergesort(int[] arr, int left, int right, int[] temp) {
- if (left < right) {
- int mid = (left + right) / 2;
- mergesort(arr, left, mid, temp);
- mergesort(arr, mid + 1, right, temp);
- mergeSort(arr, left, mid, right, temp);
- }
- }
-
- public static void mergeSort(int[] arr, int left, int mid, int right, int[] temp) {
- int i = left;
- int j = mid + 1;
- int t = 0;
-
- while (i <= mid && j <= right) {
- if (arr[i] <= arr[j]) {
- temp[t] = arr[i];
- i += 1;
- t += 1;
- } else {
- temp[t] = arr[j];
- j += 1;
- t += 1;
- }
- }
- while (i <= mid) {
- temp[t] = arr[i];
- t += 1;
- i += 1;
- }
- while (j <= right) {
- temp[t] = arr[j];
- t += 1;
- j += 1;
- }
- t = 0;
- int tempLeft = left;
- while (tempLeft <= right) {
- arr[tempLeft] = temp[t];
- tempLeft += 1;
- t += 1;
- }
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
-
- int n = sc.nextInt();
-
- int[] arr = new int[n];
-
- for (int i = 0; i < n; i++) {
- arr[i] = sc.nextInt();
- }
- quickSort(arr, 0, arr.length - 1);
- System.out.println(Arrays.toString(arr));
- }
-
- public static void quickSort(int[] arr, int left, int right) {
- int l = left;
- int r = right;
- int pivot = arr[(left + right) / 2];
- while (l < r) {
- while (arr[l] < pivot) {
- l += 1;
- }
- while (arr[r] > pivot) {
- r -= 1;
- }
- if (l >= r) {
- break;
- }
- int temp = arr[l];
- arr[l] = arr[r];
- arr[r] = temp;
- if (arr[l] == pivot) {
- r -= 1;
- }
- if (arr[r] == pivot) {
- l += 1;
- }
- }
- if (l == r) {
- l += 1;
- r -= 1;
- }
- if (left < r) {
- quickSort(arr, left, r);
- }
- if (right > l) {
- quickSort(arr, l, right);
- }
-
- }
- public static void main(String[] args) {
-
- int[] arr = { 2, 1, 7, 6, -1, 5 };
-
- Date date = new Date();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateStr = simpleDateFormat.format(date);
- System.out.println(dateStr);
-
- heapSort(arr);
-
- Date date1 = new Date();
- SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateStr1 = simpleDateFormat1.format(date);
- System.out.println(dateStr1);
- System.out.println(Arrays.toString(arr));
- }
-
- public static void heapSort(int[] arr) {
- for (int i = arr.length / 2 - 1; i >= 0; i--) {
- adjustHeap(arr, i, arr.length);
- }
- for (int j = arr.length - 1; j > 0; j--) {
- int temp = arr[j];
- arr[j] = arr[0];
- arr[0] = temp;
- adjustHeap(arr, 0, j);
- }
- }
-
- public static void adjustHeap(int[] arr, int i, int length) {
- int temp = arr[i];
- for (int j = i * 2 + 1; j < length; j = j * 2 + 1) {
- if (j + 1 < length && arr[j] < arr[j + 1]) {
- j++;
- }
- if (arr[j] > temp) {
- arr[i] = arr[j];
- i = j;
- } else {
- break;
- }
- }
- arr[i] = temp;
- }
- public static void main(String[] args) {
- int a[] = { 100, 93, 97, 92, 96, 99, 92, 89, 93, 97, 90, 94, 92, 95 };
- int b[] = countSort(a);
- for (int i : b) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
-
- public static int[] countSort(int[] a) {
- int b[] = new int[a.length];
- int max = a[0], min = a[0];
- for (int i : a) {
- if (i > max) {
- max = i;
- }
- if (i < min) {
- min = i;
- }
- }
- int k = max - min + 1;
- int c[] = new int[k];
- for (int i = 0; i < a.length; ++i) {
- c[a[i] - min] += 1;
- }
- for (int i = 1; i < c.length; ++i) {
- c[i] = c[i] + c[i - 1];
- }
- for (int i = a.length - 1; i >= 0; --i) {
- b[--c[a[i] - min]] = a[i];
- }
- return b;
- }
- public static void main(String[] args) {
-
- int[] arr = { 3, 1, 10, 9, 20 };
- bucketSort(arr);
-
- for (int i : arr) {
- System.out.print(i + " ");
- }
-
- }
-
- public static void bucketSort(int arr[])
- {
- int n = arr.length;
- int bask[][] = new int[10][n];
- int index[] = new int[10];
- int max = Integer.MIN_VALUE;
- for (int i = 0; i < n; i++) {
- max = max > (Integer.toString(arr[i]).length()) ? max : (Integer.toString(arr[i]).length());
- }
- String str;
- for (int i = max - 1; i >= 0; i--) {
- for (int j = 0; j < n; j++) {
- str = "";
- if (Integer.toString(arr[j]).length() < max) {
- for (int k = 0; k < max - Integer.toString(arr[j]).length(); k++)
- str += "0";
- }
- str += Integer.toString(arr[j]);
- bask[str.charAt(i) - '0'][index[str.charAt(i) - '0']++] = arr[j];
- }
- int pos = 0;
- for (int j = 0; j < 10; j++) {
- for (int k = 0; k < index[j]; k++) {
- arr[pos++] = bask[j][k];
- }
- index[j] = 0;
- }
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = { 3, 1, 10, 9, 20 };
- radixSort(arr);
- System.out.println(Arrays.toString(arr));
- }
-
- public static void radixSort(int[] arr) {
- int max = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if (arr[i] > max) {
- max = arr[i];
- }
- }
- int maxLength = (max + "").length();
-
- int[][] bucket = new int[10][arr.length];
- int[] bucketElementCounts = new int[10];
-
- for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
- for (int j = 0; j < arr.length; j++) {
- int digitOfElement = arr[j] / n % 10;
- bucket[digitOfElement ][bucketElementCounts[digitOfElement]] = arr[j];
- bucketElementCounts[digitOfElement]++;
- }
-
- int index = 0;
- for (int k = 0; k < bucketElementCounts.length; k++) {
- if (bucketElementCounts[k] != 0) {
- for (int l = 0; l < bucketElementCounts[k]; l++) {
- arr[index++] = bucket[k][l];
- }
- }
- bucketElementCounts[k] = 0;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。