输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: ,元素大小满足
第一行输入数组元素个数第二行输入待排序的数组,每个数用空格隔开第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出排好序的数字
8 1 2 4 9 3 55 64 25 0
1 2 3 4 9 25 55 64
5 1 2 3 4 5 1
5 4 3 2 1
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int N = in.nextInt(); Integer[] a = new Integer[N]; for(int i=0;i<N;i++){ a[i] = in.nextInt(); } int flag = in.nextInt(); sortIntegerArray(a,flag); } in.close(); } public static void sortIntegerArray(Integer[] a,int flag){ if(flag==0){ //升序 Arrays.sort(a); }else{ //降序 Arrays.sort(a,new MyComparator()); } for(int i=0;i<a.length;i++){ if(i==a.length-1) System.out.print(a[i]); else System.out.print(a[i]+" "); } System.out.println(); } } class MyComparator implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2) { if(o1.intValue()<o2.intValue()) return 1; else if(o1.intValue()==o2.intValue()) return 0; else return -1; } }
#include <iostream> using namespace std; int main() { int Array_Number,Array[10000],iSortFlag,temp; while(cin>>Array_Number) { for(int i=0;i<Array_Number;i++) cin>>Array[i]; cin>>iSortFlag; if(iSortFlag==1)//降序排序 { for(int j=0;j<Array_Number;j++) { for(int k=0;k<Array_Number-j-1;k++) { if(Array[k]<Array[k+1]) { temp=Array[k]; Array[k]=Array[k+1]; Array[k+1]=temp; } } } } else//升序排序 { for(int j=0;j<Array_Number;j++) { for(int k=0;k<Array_Number-j-1;k++) { if(Array[k]>Array[k+1]) { temp=Array[k]; Array[k]=Array[k+1]; Array[k+1]=temp; } } } } //输出排序后的数组 for(int i=0;i<Array_Number-1;i++) cout<<Array[i]<<' '; cout<<Array[Array_Number-1]<<endl; } }
#include <stdio.h> int count; void sortIntegerArray(int *array, int flag) { int temp; int i, j; for(i = 0; i < count; i++ ) { for(j = i + 1; j <= count -1; j++) { if(0 == flag) { if(array[i] > array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; } } else { if(array[i] < array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; } } } } } int main(int argc, char *argv[]) { int i; int flag; int array[1024] = {0}; while(scanf("%d", &count) != EOF) { for(i = 0; i < count; i++) { scanf("%d", &array[i]); } scanf("%d", &flag); sortIntegerArray(array, flag); for(i = 0; i < count; i++) { if(i == count - 1 ) { printf("%d\n", array[i]); break; } printf("%d ", array[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(int a,int b)//从大到小排序 { return a>b; } int main() { int n=0,num; cin>>n;//输入数组的个数 vector<int>result; while(n--)//每次输入一个数据就减少一个 { cin>>num;// result.push_back(num); } int a; cin>>a;//输入0/1 if(a==0)//如果是0则升序 { sort(result.begin(),result.end()); for(auto i:result) cout<<i<<" "; cout<<endl; } else//如果是1则降序 { sort(result.begin(),result.end(),cmp); for(auto i:result) cout<<i<<" "; cout<<endl; } return 0; }
#include<stdio.h> #include<string.h> int main() { int n; while(scanf("%d",&n)!=EOF) { int two[n]; scanf("%d",&two[0]); int thr; if(scanf("0",&thr)) { for(int i=1;i<n;i++) { scanf("%d",&two[i]); for (int j=i;j>0;j--) { if(two[j]<two[j-1]) { int two0=two[j]; two[j]=two[j-1]; two[j-1]=two0; } else { break; } } printf("%d",&two[i]); } } else { for(int i=1;i<n;i++) { scanf("%d",&two[i]); for (int j=i;j>0;j--) { if(two[j]>two[j-1]) { int two1=two[j]; two[j]=two[j-1]; two[j-1]=two1; } else { break; } } printf("%d",&two[i]); } } } return 0; }这样写问题出在哪,还是就不支持这么写
#include<iostream> using namespace std; int main() { int n;//数组元素 cin>>n; int a[n]; int flag;//0&nbs***bsp;1 for(int i=0;i<n;i++) { cin>>a[i]; } cin>>flag; int temp; if(flag==0)//升序 { for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } if(flag==1)//降序 { for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(a[j]<a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } for(int i=0;i<n;i++) { cout<<a[i]<<' '; } return 0; }
import java.util.*; public class Main{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int len = sc.nextInt(); int[] num = new int[len]; for (int i = 0; i < len; i++) { num[i] = sc.nextInt(); } int mode = sc.nextInt(); Arrays.sort(num); if (mode == 0){ for (int i = 0; i < len; i++) System.out.print(num[i]+" "); } else { for (int i = len - 1; i >= 0; i--) System.out.print(num[i]+" "); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); String[] split = br.readLine().split(" "); int[] arr = new int[split.length]; for (int i = 0; i < split.length; i++) { arr[i] = Integer.parseInt(split[i]); } String s1 = br.readLine(); if (s1.equals("0")) { quickSortIncrease(arr, 0, arr.length - 1); print(arr); } else { quickSortDecrease(arr, 0, arr.length - 1); print(arr); } } private static void quickSortIncrease(int[] arr, int left, int right) { if (left >= right) return; int i = left - 1, j = right + 1, mid = arr[(left + right) >> 1]; while (i < j) { do i++; while (mid > arr[i]); do j--; while (mid < arr[j]); if (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } quickSortIncrease(arr, left, j); quickSortIncrease(arr, j + 1, right); } private static void quickSortDecrease(int[] arr, int left, int right) { if (left >= right) return; int i = left - 1, j = right + 1, mid = arr[(left + right) >> 1]; while (i < j) { do i++; while (mid < arr[i]); do j--; while (mid > arr[j]); if (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } quickSortDecrease(arr, left, j); quickSortDecrease(arr, j + 1, right); } private static void print(int[] arr) { for (int i : arr) { System.out.print(i + " "); } } }
def quick_sort(l,order): if len(l)<=1: return l else: i=-1;j=0 pivot=l[-1] while j<=len(l)-2: if eval('l[j]{}pivot'.format('>' if order==0 else '<')): j+=1 else: i+=1 temp=l[j];l[j]=l[i];l[i]=temp#完成一次交换 j+=1 temp=pivot;l[-1]=l[i+1];l[i+1]=temp#完成一次交换 left=quick_sort(l[:i+1],order);right=quick_sort(l[i+2:],order) return left+[l[i+1]]+right while True: try: n=int(input()) l=list(map(int,input().split())) order=int(input()) #利用快慢指针进行快速排序,快指针j和慢指针i #j从第一个数扫描到pivot前面的一个数 #快指针j每到一个数,判断该数是否小于pivot,若小于,则慢指针i进一位,并交换i、j对应的数字 #完成一轮遍历后,i及其之前的数字都比pivot小,i之后的数字都比pivot大 #因此,将i+1上的数字和pivot的数字交换,再对左右两边的序列继续排序即可 l=quick_sort(l,order) print(*l) except: break
def partition(nums, left, right, method): import random rand = random.randint(left,right) nums[left],nums[rand] = nums[rand],nums[left] tmp = nums[left] if method == '0': while left < right: while left < right and nums[right] >= tmp: right -= 1 nums[left] = nums[right] while left < right and nums[left] <= tmp: left += 1 nums[right] = nums[left] nums[left] = tmp return left if method == '1': while left < right: while left < right and nums[right] <= tmp: right -= 1 nums[left] = nums[right] while left < right and nums[left] >= tmp: left += 1 nums[right] = nums[left] nums[left] = tmp return left def quick_sort(nums, left, right, method): if left < right: mid = partition(nums, left, right, method) quick_sort(nums, left, mid-1, method) quick_sort(nums, mid+1, right, method) while True: try: n = int(input()) nums = list(map(int, input().split())) method = input() quick_sort(nums, 0, n-1, method) print(*nums) except: break
# 方法一: while True: try: count, arr = input(), map(int, input().strip().split(' ')) flag = input().strip() if flag == '0': arr = sorted(arr) # print(arr) if flag == '1': arr = sorted(arr, reverse=True) # print(arr) print(' '.join(map(str, arr))) except: break # 方法二:(reverse=True可以直接写成reverse=1) while True: try: count, arr = input(), map(int, input().strip().split(' ')) flag = int(input().strip()) arr = sorted(arr, reverse=flag) print(' '.join(map(str, arr))) except: break
#include<bits/stdc++.h> using namespace std; int main() { int n,a[10000],flag,tmp; /**********输入**********/ while(cin>>n) { for(int i=0;i<n;i++) { cin>>a[i]; } cin>>flag; /**********冒泡排序**********/ for(int j=0;j<n-1;j++) for(int k=j+1;k<n;k++) { if(a[j]>a[k]) { tmp=a[j]; a[j]=a[k]; a[k]=tmp; } } /**********升序输出**********/ if(flag==0) { for(int i=0;i<n-1;i++) { cout<<a[i]<<" "; } cout<<a[n-1]; } /**********降序输出**********/ if(flag==1) { for(int i=n-1;i>0;i--) { cout<<a[i]<<" "; } cout<<a[0]; } cout<<endl; } return 0; }
#include<stdio.h> void sort(int A[], int N) { int i, j, inc; int tmp; for(inc = N / 2; inc > 0; inc /= 2){ for(i = inc; i < N; i++){ tmp = A[i]; for(j = i; j >= inc; j -= inc){ if(A[j - inc] > tmp) A[j] = A[j - inc]; else break; } A[j] = tmp; } } } int main(int argc, char **argv) { int N = 0; int i = 0; while(scanf("%d", &N) != EOF){ int flag; int *A = (int *)malloc(sizeof(int) * N); for(i = 0; i < N; ++i) scanf("%d", A + i); scanf("%d", &flag); sort(A, N); if(flag == 0){ for(i = 0; i < N; ++i){ if(i < N - 1) printf("%d ", A[i]); else printf("%d\n", A[i]); } } else{ for(i = N - 1; i >= 0; --i){ if(i > 0) printf("%d ", A[i]); else printf("%d\n", A[i]); } } free(A); } }
while True: try: def quick_sort(arr, left, right): """快速排序""" if left >= right: return arr key = arr[left] low = left high = right while left < right: while left < right and arr[right] >= key: right -= 1 arr[left] = arr[right] while left < right and arr[left] <= key: left += 1 arr[right] = arr[left] arr[right] = key quick_sort(arr, low, left - 1) quick_sort(arr, left + 1, high) return arr total_num = int(input()) input_list = list(map(int, input().strip().split())) descending = int(input()) if len(input_list) == 1: print(input_list[0]) if descending == 0: print(" ".join(list(map(str, quick_sort(input_list, 0, len(input_list) - 1))))) else: print(" ".join(list(map(str, quick_sort(input_list, 0, len(input_list) - 1)[::-1])))) except: break
快速排序实现基本功能,通过列表索引实现逆序