import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(s[i]); } Arrays.sort(a); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } } }
import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; public class Main { static int[] array; static int n; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); array = new int[n]; for (int i = 0; i < n; i++) array[i] = scanner.nextInt(); // apiSort(); // bubbleSort(); // selectSort(); // insertSort(); // heapSort(); // mergeSort(0, n - 1); quickSort(0, n - 1); for (int i : array) System.out.print(i + " "); } // 使用Java APi static void apiSort() { Arrays.sort(array); } // 交换两个数 static void swap(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } // 冒泡排序 static void bubbleSort() { // 每趟往前归位一个最小值 for (int i = 1; i < n; i++) for (int j = n - 1; j >= i; j--) if (array[j] < array[j - 1]) swap(j, j - 1); } // 选择排序 static void selectSort() { for (int i = 0; i < n - 1; i++) { int k = i; for (int j = i; j <= n - 1; j++) if (array[j] < array[k]) k = j; swap(k, i); } } // 插入排序 static void insertSort() { for (int i = 1; i < n; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) swap(j, j - 1); else break; } } } // 堆排序 static void heapSort() { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for (int i : array) minHeap.add(i); for (int i = 0; i < n; i++) array[i] = minHeap.poll(); } // 归并排序 static void mergeSort(int i, int j) { if (i >= j) return; int mid = (i + j) >> 1; mergeSort(i, mid); mergeSort(mid + 1, j); merge(i, mid, j); } static void merge(int i, int mid, int j) { int[] temp = new int[n]; int l = i; int r = mid + 1; int t = i; while (l <= mid && r <= j) temp[t++] = array[l] <= array[r] ? array[l++] : array[r++]; while (l <= mid) temp[t++] = array[l++]; while (r <= j) temp[t++] = array[r++]; System.arraycopy(temp, i, array, i, j - i + 1); } // 快速排序 static void quickSort(int i, int j) { if (i >= j) return; int pivot = partition(i, j); quickSort(i, pivot - 1); quickSort(pivot + 1, j); } static int partition(int i, int j) { int v = array[i]; int l = i + 1; int r = j; while (true) { while (l <= j && array[l] <= v) l++; while (r >= i + 1 && array[r] >= v) r--; if (l > r) break; swap(l++, r--); } swap(i, r); return r; } }
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] nums = new int[n]; for(int i=0;i<n;i++){ nums[i] = sc.nextInt(); } Arrays.sort(nums); for(int i : nums){ System.out.print(i+" "); } } } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
int[] data = new int[n];
for (int i = 0; i < data.length; i++) {
data[i] = scan.nextInt();
}
shellSort(data);
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}
}
private static void shellSort(int[] data) {
int len = data.length;
int i, j, temp;
for (int half = len / 2; half > 0; half /= 2) {
for (i = half; i < len; i++) {
temp = data[i];
j = i - half;
while (j >= 0 && temp < data[j]) {
data[j + half] = data[j];
j -= half;
}
data[j + half] = temp;
}
}
}
}
import java.util.*; public class Main{ public static int[] aux; public static void bubbleSort(int[] arr){ int len = arr.length; for(int i = len - 1; i >= 0; i--){ for(int j = 0; j < i; j++){ if(arr[j] > arr[j + 1]){ int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } } public static void selectSort(int[] arr){ int len = arr.length; for(int i = 0; i < len; i++){ int min = i; for(int j = i + 1; j < len; j++){ if(arr[min] > arr[j]) min = j; } int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } public static void insertSort(int[] arr){ int len = arr.length; for(int i = 0; i < len - 1; i++){ for(int j = i + 1; j > 0; j--){ if(arr[j - 1] > arr[j]){ int temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } } public static void quickSort(int[] arr, int low, int high){ if(low >= high){ return; } int i = low; int j = high; int pivot = arr[i]; while(i < j){ while(i < j && pivot <= arr[j]) j--; arr[i] = arr[j]; while(i < j && pivot >= arr[i]) i++; arr[j] = arr[i]; } arr[i] = pivot; quickSort(arr, low, i - 1); quickSort(arr, i + 1, high); } public static void shellSort(int[] arr){ int len = arr.length; int gap = 1; while(gap < len / 3) gap = gap * 3 + 1;//塞奇书中提出的步长选取策略,平均复杂度能达到nlogn for(; gap >= 1; gap /= 3){ for(int i = 0; i < len - gap; i += gap){ for(int j = i + gap; j > 0; j -= gap){ if(arr[j - gap] > arr[j]){ int temp = arr[j - gap]; arr[j - gap] = arr[j]; arr[j] = temp; } } } } } public static void mergeSort(int[] arr, int low, int high){ if(low >= high){ return; } int mid = (low + high) / 2; mergeSort(arr, low, mid); mergeSort(arr, mid + 1, high); merge(arr, low, high); } public static void merge(int[] arr, int low, int high){ int mid = (low + high) / 2; int i = low; int j = mid + 1; for(int k = low; k <= high; k++){ aux[k] = arr[k]; } for(int k = low; k <= high; k++){ if(i > mid){ arr[k] = aux[j++]; }else if(j > high){ arr[k] = aux[i++]; }else if(aux[i] < aux[j]){ arr[k] = aux[i++]; }else{ arr[k] = aux[j++]; } } } public static void heapifySort(int[] arr){ int len = arr.length; for(int i = len - 1; i > 0; i--){//此处不可写为i >= 0 maxHeapify(arr, i); int temp = arr[i]; arr[i] = arr[0]; arr[0] = temp; } } public static void maxHeapify(int[] arr, int n){ for(int i = (n - 1) / 2; i >= 0; i--){ int child = 2 * i + 1; if(child != n && arr[child] < arr[child + 1]){ child++; } if(arr[i] < arr[child]){ int temp = arr[i]; arr[i] = arr[child]; arr[child] = temp; } } } 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(); } //bubbleSort(arr); //selectSort(arr); //insertSort(arr); //quickSort(arr, 0, arr.length - 1); //shellSort(arr); //aux = new int[arr.length]; //mergeSort(arr, 0, arr.length - 1); heapifySort(arr); for(int i = 0; i < n; i++){ System.out.print(String.valueOf(arr[i]) + " "); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int n = sc.nextInt(); int[] sort = new int[n]; for (int i = 0; i < n; i++){ sort[i] = sc.nextInt(); } for (int i = 0; i < n-1; i++){ for (int j = i+1; j < n; j++){ if (sort[i] > sort[j]){ int temp = sort[j]; sort[j] = sort[i]; sort[i] = temp; } } } for(int i = 0; i < n; i++){ System.out.print(sort[i] + " "); } System.out.println(); } } }
// insertSort import java.util.Scanner; /** * Created by Yuan on 2017/3/2. */ public class SortTest { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = scanner.nextInt(); } insertSort(data); for (int i=0;i<n-1;i++){ System.out.print(data[i]+" "); } System.out.println(data[n-1]); } } public static void insertSort(int[] data){ int len= data.length; for(int i=0;i<len-1;i++){ int j=i; int temp=data[i+1]; while (j>=0&&temp<data[j]){ data[j+1]=data[j]; j--; } data[j+1]=temp; } } }