插入排序:
- 设置一个first_unsorted表示第一个未排序的下标
- 每次将first_unsorted向前移位到合适的位置
// Problem#: 1154
// Submission#: 1024358
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
void Insertion_sort(int arr[], int size)
{
int position;
int first_unsorted;
int current;
for(first_unsorted = 1; first_unsorted < size; first_unsorted++)
{
if(arr[first_unsorted - 1] > arr[first_unsorted])
{
position = first_unsorted;
current =arr[first_unsorted];
do //shift all entries until the proper position is found
{
arr[position] = arr[position - 1];
position --;
}while(position > 0 && arr[position - 1] > current); //after the cycle, entry[position - 1] <= current
arr[position] = current;
}
}
}
void output(int arr[], int size)
{
for(int i = 0; i < size; i++)
printf("%d\n", arr[i]);
}
int main()
{
int test_num;
scanf("%d", &test_num);
int size;
int entry[1000];
for(int i = 0; i < test_num; i++)
{
scanf("%d", &size);
for(int j = 0; j < size; j++)
{
scanf("%d", &entry[j]);
}
int position = 0;
Insertion_sort(entry, size);
output(entry, size);
}
return 0;
}
选择排序:
- 每一次循环都是选择最小、大的一个元素放到数组的最后
// Problem#: 1154
// Submission#: 1024443
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
void Swap(int arr[], int first, int second)
{
int temp;
temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
void Select_sort(int arr[], int size)
{
for(int position = size - 1; position > 0; position --)
{
int largest, current;
largest = 0;
for(current = 1; current <= position; current ++)
{
if(arr[current] > arr[largest])
{
largest = current;
}
}
Swap(arr, largest, position);
}
}
void output(int arr[], int size)
{
for(int i = 0; i < size; i++)
printf("%d\n", arr[i]);
}
int main()
{
int test_num;
scanf("%d", &test_num);
int size;
int entry[1000];
for(int i = 0; i < test_num; i++)
{
scanf("%d", &size);
for(int j = 0; j < size; j++)
{
scanf("%d", &entry[j]);
}
int position = 0;
Select_sort(entry, size);
output(entry, size);
}
return 0;
}
快速排序:
- 关键在于将数组元素分为两部分
- 选择中间的元素当为“主元”,比“主元”小的放一边,比“主元”大的放另外一边,再分别对两边进行快速排序
// Problem#: 1154
// Submission#: 1024411
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
void Swap(int arr[], int first, int second)
{
int temp;
temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
int partition(int arr[],int low,int high)
{
int pivot;
int i, //Used to scan through the array
last_small; //Position of the last key less than pivot
Swap(arr, low, (low + high)/2);
pivot = arr[low];
last_small = low;
for(i = low + 1; i <= high; i++)
{
if(pivot > arr[i] )
{
last_small = last_small + 1;
Swap(arr,last_small, i);
}
}
Swap(arr,low, last_small); // Put the pivot into tts proper position
return last_small;
}
void Quick_sort(int arr[], int low, int high)
{
int pivot_position;
if(low < high)
{
pivot_position = partition(arr, low, high);
Quick_sort(arr, low, pivot_position - 1); //it will ignore the entry[pivot_position] because it has benn at its proper position
Quick_sort(arr, pivot_position + 1, high);
}
}
void output(int arr[], int size)
{
for(int i = 0; i < size; i++)
printf("%d\n", arr[i]);
}
int main()
{
int test_num;
scanf("%d", &test_num);
int size;
int entry[1000];
for(int i = 0; i < test_num; i++)
{
scanf("%d", &size);
for(int j = 0; j < size; j++)
{
scanf("%d", &entry[j]);
}
int position = 0;
Quick_sort(entry, 0, size - 1);
output(entry, size);
}
return 0;
}
冒泡排序:
- 每次都将最大的移到后面
// Problem#: 1154
// Submission#: 1024347
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<stdio.h>
int main()
{
int n,i,j,k,t,a[1000],temp,m;
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d",&n);
for(k=0;k<n;k++)
scanf("%d",&a[k]);
for(m=0;m<n-1;m++)
{
for(i=0;i<n-1-m;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
return 0;
}