赞
踩
- int[] nums = {5,6,2,1,9,3,4,10};
- for (int i = 0; i < nums.length; i++) {
- for (int j = 0; j < nums.length - i - 1; j++) { // 注意这里需要减去i
- // 如果左边的大于右边的值 则交换
- if (nums[j+1] < nums[j]) {
- int t = nums[j];
- nums[j] = nums[j+1];
- nums[j + 1] = t;
- }
- }
- }
- int[] nums = {5,6,2,1,9,3,4,10};
- int temp;
- for (int i = 1; i < nums.length; i++) {
- for (int j = i; (j > 0) && (nums[j]<nums[j-1]) ; j--) {
- temp = nums[j];
- nums[j] = nums[j-1];
- nums[j-1] = temp;
- }
- }
- int[] nums = {5,6,2,1,9,3,4,10};
- for(int i = 0;i<nums.length;i++)
- {
- int minIndex = i;
- for(int j = i;j<nums.length;j++)
- {
- if(nums[minIndex]>nums[j]) {
- minIndex = j;
- }
- }
- int temp = nums[minIndex];
- nums[minIndex] = nums[i];
- nums[i] = temp;
- }
- int[] nums = {5,6,2,1,9,3,4,10};
- int n = nums.length;
- int min = nums[0];
- int max = nums[0];
- for (int i = 1; i < n; i++){
- if (max < nums[i]){
- max = nums[i];
- }
- if (nums[i] < min){
- min = nums[i];
- }
- }
- int len = max - min + 1;
- int[] temp = new int[len];
- for (int i = 0; i < n; i++) {
- temp[nums[i] - min]++;
- }
- int k = 0;
- for (int i = 0; i < len; i++) {
- for (int j = temp[i]; j > 0; j--){
- nums[k] = i + min;
- k++;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。