//
// main.m
// OrderTest
//
// Created by Roeru on 23/7/14.
// Copyright (c) 2014 Shinoi. All rights reserved.
//
#import <Foundation/Foundation.h>
void noNameSort(int a[],int m,int n)
{
int l;
int temp;
if (m < n) {
temp = a[m];
for (l = m - 1; l >= 0 && a[l] > temp; l--) {
a[l + 1] = a[l];
a[l] = temp;
}
noNameSort(a,m+1,n);
}
}
void quickSort(int a[], int l, int r)
{
if (l < r)
{
int i = l, j = r, x = a[l];
while (i < j)
{
while(i < j && a[j] >= x)
{
j--;
}
if(i < j)
{
a[i] = a[j];
i = i + 1;
}
while(i < j && a[i] < x)
{
i++;
}
if(i < j)
{
a[j] = a[i];
j = j - 1;
}
}
a[i] = x;
quickSort(a, l, i - 1);
quickSort(a, i + 1, r);
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
int i;
NSLog(@"How many number du you want");
scanf("%d",&i);
int a[i - 1];
for (int j = 0; j < i; j ++) {
NSLog(@"a[%d] = ?",j);
scanf("%d",&a[j]);
}
for (int j = 0; j < i; j ++) {
NSLog(@"a[%d] = %d",j,a[j]);
}
NSLog(@"Which order do you want");
NSLog(@"1.Exchange order");
NSLog(@"2.Insert order");
NSLog(@"3.Selectionsort");
NSLog(@"4.Shell");
NSLog(@"5.CocktailSort");
NSLog(@"6.QuickSort");
NSLog(@"7.NonameSort");
int choose;
scanf("%d",&choose);
int k,l;
int temp; //存放中间变量
if (1 == choose)
{
for (k = 0; k < i ; k++) {
for (l = 0; l < i - 1; l ++) {
if (a[l] > a[l + 1]) {
temp = a[l];
a[l] = a[l + 1];
a[l + 1] = temp;
}
}
}
}
if (2 == choose) {
for (k =1 ; k < i; k++) {
temp = a[k];
for (l = k - 1; l >= 0 && a[l] > temp; l--) {
a[l + 1] = a[l];
a[l] = temp;
}
}
}
if (3 == choose) {
for (k = 0; k < i - 1; k++) {
for (l = k + 1 ; l < i; l++) {
if (a[k] > a[l]) {
temp = a[k];
a[k] = a[l];
a[l] = temp;
}
}
}
}
if (4 == choose) {
k = i / 2;
while (k > 0) {
for (int n = 0; n + k < i; n++) {
temp = a[n];
if (temp > a[n + k]) {
a[n] = a[n + k];
a[n + k] = temp;
}
}
k = k - 1;
}
}
if (5 == choose) {
int bottom = 0;
int top = i - 1;
int result = 1;
int m,n;
while (result) {
for (n = bottom; n < top; n++) {
if (a[n] > a[n + 1]) {
temp = a[n];
a[n] = a[n + 1];
a[n + 1] = temp;
result = 2;
}
}
bottom++;
if (bottom == top) {
result = 0;
break;
}
for (m = top; m > 0; m--) {
if (a[m] < a[m - 1]) {
temp = a[m];
a[m] = a[m-1];
a[m-1] = temp;
}
}
top--;
if (bottom == top) {
result = 0;
break;
}
}
}
if (6 == choose) {
quickSort(a, 0, i - 1);
}
if (7 == choose) {
noNameSort(a,1,i);
}
NSLog(@"after the order");
for (int j = 0; j < i; j ++) {
NSLog(@"a[%d] = %d",j,a[j]);
}
}
return 0;
}
原文地址:http://my.oschina.net/roeru/blog/294758