#import "SortFunction.h"
1.//冒泡排序(bubble sort) — O(n2)
void bubbleSort(int array[],int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (array[j] < array[j+1]) {
swap(array + j,array + j + 1);
}
}
}
}
2.//选择排序(Selection sort) - O(n2)
void selectionSort(int array[],int count){
for (int i = 0;i < count - 1 ; i++) {
int minIndex = i;
for (int j = i + 1; j < count; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) { //如何最小值索引与 i 不同的机会多不用判断效率更高
swap(array + i, array + minIndex);
}
}
}
3.//插入排序 (insertion sort)— O(n2)
void insertionSort(int array[],int count){
for (int i = 1; i < count; i++) {
int temp = array[i];
int j = i - 1;
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
4.//合併排序(merge sort)— O(n log n); 需要 O(n) 額外空間(两路归并)
void mergeSort2w(int array[],int count){
int mid = count / 2;
int temp[count];
int * pLeft = temp;
int * pRight = temp + mid;
int leftCount = mid;
int rightCount = count - mid;
for (int i = 0; i < count; i++) {
temp[i] = array[i];
}
bubbleSort(pLeft, leftCount);
bubbleSort(pRight, rightCount);
//开始归并排序
int i = 0; //排序数组索引
int j = 0; //左数组索引
int k = 0; //右数组索引
while (j < leftCount && k < rightCount) {
if (pLeft[j] < pRight[k]) {
array[i++] = pLeft[j++];
}else{
array[i++] = pRight[k++];
}
}
while (j < leftCount) {
array[i++] = pLeft[j++];
}
while (k < rightCount) {
array[i++] = pRight[k++];
}
}
5.//快速排序(quicksort)— O(n log n) 期望時間, O(n2) 最壞情況; 對於大的、亂數串列一般相信是最快的已知排序
void quickSort(int array[],int count){
if (count > 1) {
int i = 0;
int j = count - 1;
int temp = array[i];
while (i < j) {
while (i < j && array[j] >= temp) { //向右查找比temp小的值
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (i < j && array[i] < temp) { //向左查找比temp大的值,不能有等于
i++;
}
if (i < j) {
array[j--] = array[i];
}
array[i] = temp;
}
quickSort(array, i);
quickSort(array + i + 1, count - i - 1);
}
}
6.//交换两个数的值
void swap(int * x,int * y){
int temp = 0;
temp = *x;
*x = *y;
*y = temp;
}
7.//遍历数组输出
void printArray(int array[],int count){
for (int i = 0 ; i < count; i++) {
printf("%d : %d \n",i,array[i]);
}
}
8.//折半查找,时间复杂度为o(log(n))
int halfSeek(int array[],int count,int number){
int start = 0;
int end = count - 1;
int mid = (end + start) / 2;
while (array[mid] != number && start < end) {
if (array[mid] > number) {
end = mid - 1;
}else{
start = mid + 1;
}
mid = (end + start) / 2;
}
if (array[mid] == number) {
printf("%d\n",mid);
return mid;
}else{
printf("not found\n");
}
return -1;
}
int a[5]={0};
printf("input 5 numbers \n");
for ( int i =0; i<5;i++) {
scanf("%d",&a[i]);
}
for (int i =0; i<5; i++) {
printf("%d \n",a[i]);
}
1.//冒泡排序(bubble sort) — O(n2)
void bubbleSort(int array[],int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (array[j] < array[j+1]) {
swap(array + j,array + j + 1);
}
}
}
}
2.//选择排序(Selection sort) - O(n2)
void selectionSort(int array[],int count){
for (int i = 0;i < count - 1 ; i++) {
int minIndex = i;
for (int j = i + 1; j < count; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) { //如何最小值索引与 i 不同的机会多不用判断效率更高
swap(array + i, array + minIndex);
}
}
}
3.//插入排序 (insertion sort)— O(n2)
void insertionSort(int array[],int count){
for (int i = 1; i < count; i++) {
int temp = array[i];
int j = i - 1;
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
4.//合併排序(merge sort)— O(n log n); 需要 O(n) 額外空間(两路归并)
void mergeSort2w(int array[],int count){
int mid = count / 2;
int temp[count];
int * pLeft = temp;
int * pRight = temp + mid;
int leftCount = mid;
int rightCount = count - mid;
for (int i = 0; i < count; i++) {
temp[i] = array[i];
}
bubbleSort(pLeft, leftCount);
bubbleSort(pRight, rightCount);
//开始归并排序
int i = 0; //排序数组索引
int j = 0; //左数组索引
int k = 0; //右数组索引
while (j < leftCount && k < rightCount) {
if (pLeft[j] < pRight[k]) {
array[i++] = pLeft[j++];
}else{
array[i++] = pRight[k++];
}
}
while (j < leftCount) {
array[i++] = pLeft[j++];
}
while (k < rightCount) {
array[i++] = pRight[k++];
}
}
5.//快速排序(quicksort)— O(n log n) 期望時間, O(n2) 最壞情況; 對於大的、亂數串列一般相信是最快的已知排序
void quickSort(int array[],int count){
if (count > 1) {
int i = 0;
int j = count - 1;
int temp = array[i];
while (i < j) {
while (i < j && array[j] >= temp) { //向右查找比temp小的值
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (i < j && array[i] < temp) { //向左查找比temp大的值,不能有等于
i++;
}
if (i < j) {
array[j--] = array[i];
}
array[i] = temp;
}
quickSort(array, i);
quickSort(array + i + 1, count - i - 1);
}
}
6.//交换两个数的值
void swap(int * x,int * y){
int temp = 0;
temp = *x;
*x = *y;
*y = temp;
}
7.//遍历数组输出
void printArray(int array[],int count){
for (int i = 0 ; i < count; i++) {
printf("%d : %d \n",i,array[i]);
}
}
8.//折半查找,时间复杂度为o(log(n))
int halfSeek(int array[],int count,int number){
int start = 0;
int end = count - 1;
int mid = (end + start) / 2;
while (array[mid] != number && start < end) {
if (array[mid] > number) {
end = mid - 1;
}else{
start = mid + 1;
}
mid = (end + start) / 2;
}
if (array[mid] == number) {
printf("%d\n",mid);
return mid;
}else{
printf("not found\n");
}
return -1;
}
9.//手动输入一个一维数组并显示结果
int main(int argc, constchar * argv[])
{int a[5]={0};
printf("input 5 numbers \n");
for ( int i =0; i<5;i++) {
scanf("%d",&a[i]);
}
for (int i =0; i<5; i++) {
printf("%d \n",a[i]);
}