// 冒泡排序
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
int arr[] = {10,11,12,13,14,15,16,17,18,19,20};
int len = sizeof(arr)/sizeof(arr[0]);
for(int i = 0;i<len-1;i++)
{
for(int j = 0;j<len - i - 1;j++)
{
if(arr[j] < arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int k = 0;k<len;k++)
{
printf("%d\n", arr[k]);
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------
// 选择排序
#import <Foundation/Foundation.h>
int main()
{
int arr[] = {10,11,12,13,14,15,16,17,18,19,20};
int len = sizeof(arr)/sizeof(arr[0]);
for(int i = 0; i < len - 1;i ++)
{
for(int j = i+1;j<len;j++)
{
if(arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0;i<len;i++)
{
printf("%d\n", arr[i]);
}
return 0;
} |
|