给定一个长度为 n 的数组,请你编写一个函数,返回该数组按升序排序后的结果。
数据范围: ,数组中每个元素都满足
要求:时间复杂度 ,空间复杂度
进阶:时间复杂度 ,空间复杂度
注:本题数据范围允许绝大部分排序算法,请尝试多种排序算法的实现。
[5,2,3,1,4]
[1,2,3,4,5]
[5,1,6,2,5]
[1,2,5,5,6]
//表达式求值 #include<stdio.h> #include <stdlib.h> int main(){ int a[5]; for (int i = 0; i < 5; i++) { scanf("%d",&a[i]); } //冒泡排序 for (int i =0; i<5; i++)//决定第几层排序 { for (int j =0; j<=4-i; j++) { if (a[j]>a[j+1]) { int l=a[j]; a[j]=a[j+1]; a[j+1]=l; } } } for (int i = 0; i < 5; i++) { printf("%d ",a[i]); } }
int* MySort(int* arr, int arrLen, int* returnSize) { int i = 0; int j = 0; int flag = 0; for (i = 0; i < arrLen - 1; i++) { flag = 1; for (j = 0; j < arrLen - i - 1; j++) { if (arr[j] > arr[j + 1]) { arr[j] ^= arr[j + 1]; arr[j + 1] ^= arr[j]; arr[j] ^= arr[j + 1]; flag = 0; } } if (flag) { break; } } *returnSize = arrLen; return arr; }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @param arrLen int arr数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* MySort(int* arr, int arrLen, int* returnSize ) { // write code here // int i,j,k; // for(i=0;i<arrLen;i++) // { // for(j=0;j<arrLen-1;j++) // { // if(arr[j]>arr[j+1]) // { // k=arr[j]; // arr[j]=arr[j+1]; // arr[j+1]=k; // } // } // } int i,j,k,min; for(i=0;i<arrLen;i++) { min=i; for(j=i;j<arrLen;j++) { if(arr[min]>arr[j]) min=j; } k=arr[min]; arr[min]=arr[i]; arr[i]=k; } * returnSize =arrLen; return arr; }
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } void QuickSort(int* arr, int begin, int end) { if (begin >= end) return; int left = begin, right = end; int key = arr[left]; int pit=left; while(left<right) { while(left<right&&arr[right]>=key) { right--; } arr[pit]=arr[right]; pit=right; while(left<right&&arr[left]<key) { left++; } arr[pit]=arr[left]; pit=left; } pit = left; arr[pit] = key; QuickSort(arr, begin, pit-1); QuickSort(arr, pit+1, end); } int* MySort(int* arr, int arrLen, int* returnSize) { // write code here QuickSort(arr, 0, arrLen - 1); *returnSize = arrLen; return arr; }
void swap(int *arr,int i,int j) { int tem = arr[i]; arr[i] = arr[j]; arr[j] = tem; } void heap(int *arr,int index,int heapsize) { if(index == heapsize) return; else { while(index<heapsize) { int tem = index; while(arr[tem]>arr[(tem-1)/2]) { swap(arr,tem,(tem-1)/2); tem = (tem-1)/2; } index++; } } } void heapify(int *arr,int index,int heapsize) { int left = 2*index+1; if(left == heapsize) { if(arr[left]>arr[index]) { swap(arr,left,index); } } else { while(left<heapsize) { if(left+1<heapsize) { int max = arr[left]>arr[left+1]?left:left+1; int largest = arr[index]>arr[max]?index:max; if(largest == index) { break; } else { swap(arr,index,largest); index = largest; } } else { if(arr[left]>arr[index]) { swap(arr,left,index); } break; } left = 2*index+1; } } } int* MySort(int* arr, int arrLen, int* returnSize ) { if(arrLen == 1) { *returnSize = arrLen; return arr; } if(arrLen == 0) { *returnSize = arrLen; return NULL; } heap(arr,0,arrLen); int i; for(i = arrLen-1;i>0;i--) { swap(arr, 0, i); heapify(arr, 0, i); } i++; swap(arr, 0, i); *returnSize = arrLen; return arr; }堆排序 时间复杂度为nlogn,空间复杂度为1;
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @param arrLen int arr数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 * * C语言声明定义全局变量请加上static,防止重复定义 */ int patition(int* arr,int low,int high) { int temp=arr[low]; while(low<high) { while(arr[high]>=temp && low<high) high--; arr[low]=arr[high]; while(arr[low]<=temp && low<high) low++; arr[high]=arr[low]; } arr[low]=temp; return low; } void quicksort(int* arr ,int low, int high) { int pivotpos; if(low<high) { pivotpos=patition(arr, low, high); quicksort(arr, low, pivotpos-1); quicksort(arr, pivotpos+1, high); } } int* MySort(int* arr, int arrLen, int* returnSize ) { // write code here //快排 quicksort(arr,0,arrLen-1); *returnSize=arrLen; return arr; }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @param arrLen int arr数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ void Quick(int *arr, int Begin, int End) { if(Begin >= End) return; int front = Begin, last = End, baseData = arr[Begin]; while(front < last) { while(arr[last] > baseData && last > front) last--; if(arr[last] < baseData) arr[front] = arr[last]; while(arr[front] < baseData && last > front) front++; if(arr[front] > baseData) arr[last] = arr[front]; } arr[last] = baseData; Quick(arr, Begin, last-1); Quick(arr, last+1, End); } int* MySort(int* arr, int arrLen, int* returnSize ) { // write code here *returnSize = arrLen; Quick(arr, 0, arrLen-1); return arr; }
int get_mid(int arr[], int L, int R) { int pivot = arr[L]; while (L < R) { while (arr[R] > pivot && L < R) R--; arr[L] = arr[R]; while (arr[L] < pivot && L < R) L++; arr[R] = arr[L]; } arr[L] = pivot; return L; } void quick_sort(int arr[], int L, int R) { if (L >= R) return; quick_sort(arr, L, get_mid(arr, L, R)); quick_sort(arr, get_mid(arr, L, R) + 1, R); } int* MySort(int* arr, int arrLen, int* returnSize ) { // write code here quick_sort(arr, 0, arrLen - 1); *returnSize = arrLen; return arr; }用qsort通过
int cmp(const void * a, const void * b) { return (*(int*)a - *(int*)b); } int* MySort(int* arr, int arrLen, int* returnSize) { // write code here qsort(arr, arrLen, sizeof(int), cmp); *returnSize = arrLen; return arr; }
int swap(int* ARR, int low, int high){ int temp; temp = ARR[high]; ARR[high] = ARR[low]; ARR[low] = temp; return 0; } int partition(int* ARR, int low, int high){ int pivotkey, m; m = low + (high - low)/2; if(ARR[low] > ARR[high]){ swap(ARR, low, high); } if(ARR[m] > ARR[high]){ swap(ARR, m, high); } if(ARR[low] < ARR[m]){ swap(ARR,low,m); } pivotkey = ARR[low]; while(low < high){ while(low < high && pivotkey <= ARR[high]) high --; swap(ARR, low, high); while(low < high && pivotkey >= ARR[low]) low ++; swap(ARR, low, high); } return low; } void Qsort(int* ARR, int low, int high){ int pivot; while(low < high){ pivot = partition(ARR, low, high); Qsort(ARR, low, pivot-1); low = pivot + 1; } } int* MySort(int* arr, int arrLen, int* returnSize ) { Qsort(arr, 0, arrLen-1); *returnSize = arrLen; return arr; }