27、排序

排序

1、选择排序

代码实现:
/*
	2017年6月19日14:46:17
	功能:选择排序的降序排序
*/
#include"stdio.h"
#define N 8
int main()
{
	int i, j, m, temp;
	int a[N];
	for(i = 0; i < N; i++)
	{
		printf("please input a number :" );
		scanf("%d",&m);
		a[i] = m;
	}

	printf("The old array is: ");
	printf("\n");
	for(i = 0; i < N; i++)
	{
		printf("%d\t",a[i] );
	}
	printf("\n" );

	for(i = 0;i < N; i++)
	{	for(j = i+1; j < N; j++)
		{
			if(a[i] < a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}

	printf("The new array is: ");
	printf("\n");
	for(i = 0; i < N; i++)
	{
		printf("%d\t",a[i] );
	}
	printf("\n" );

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果为:
	————————————————————————————————
	please input a number :2
	please input a number :4
	please input a number :8
	please input a number :9
	please input a number :6
	please input a number :1
	please input a number :9
	please input a number :5
	The old array is:
	2       4       8       9       6       1       9       5
	The new array is:
	9       9       8       6       5       4       2       1
	————————————————————————————————
*/

2、冒泡排序

代码实现:
/*
	2017年6月19日15:17:40
	功能:冒泡排序的降序排序
*/
#include"stdio.h"
#define N 8
int main()
{
	int a[N];
	int i, j, m, temp;
	for(i = 0; i < N; i++)
	{
		printf("please input a number :" );
		scanf("%d",&m);
		a[i] = m;
	}

	printf("The old array is : ");
	printf("\n");
	for(i = 0; i < N; i++)
	{
		printf("%d\t",a[i]);
	}
	printf("\n" );

	for(i = 0; i < N; i++)
	{	for(j = 0; (j < N - i)&&(j+1 < N - i); j++)
		{
			if(a[j]< a[j+1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}

	printf("The new array is : ");
	printf("\n");
	for(i = 0; i < N; i++)
	{
		printf("%d\t",a[i]);
	}
	printf("\n" );
	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果为:
	————————————————————————————————
	please input a number :2
	please input a number :4
	please input a number :8
	please input a number :4
	please input a number :9
	please input a number :5
	please input a number :3
	please input a number :1
	The old array is :
	2       4       8       4       9       5       3       1
	The new array is :
	9       8       5       4       4       3       2       1
	————————————————————————————————
*/

  

  

时间: 2024-08-03 03:15:54

27、排序的相关文章

IOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDictionary介绍

NSArray NSArray基本用法 void arrayTest1() { //数组初始化最后必须以nil结尾,表示数组元素结束 NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil]; NSLog(@"%@",array1); /*( it

优先队列实现定时器

http://www.cnblogs.com/lewiskyo/p/6359789.html 上文介绍了使用数组实现定时器,但因为插入和删除定时器的效率太低,所以这里改用优先队列实现一次. 实现代码如下: 1 #include <iostream> 2 #include <sys/time.h> 3 #include <unistd.h> 4 #include <thread> 5 #include <vector> 6 #include <

poj 1251 最小生成树模板题

Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200Sample Output 21630 prim算法 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <

*1019. 数字黑洞

1 /* 2 * Main.c 3 * 1019. 数字黑洞 4 * Created on: 2014年8月31日 5 * Author: Boomkeeper 6 *******部分通过****** 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 /* 13 *排序整数的各个位数字 14 *src[4]:原数组 15 *dstDown[4]:降序排序结果 16 *dstUp[4]:升序排序结果 17 */ 18 v

Django—Model操作

Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 Sqlite3.MySQL.PostgreSQL 等数据库,只需要在 settings.py 中配置即可,不用更改 models.py 中的代码,丰富的 API 极大的方便了使用. 一.创建表 1. 基本结构 1 from django.db import models 2 3 class UserInfo(models.Model): 4 #若不指定主键,则会自动创建int类型的id列为

hdu 1879 有的边已存在 (MST)

Sample Input31 2 1 0 //u v w 是否已建 1 3 2 02 3 4 031 2 1 01 3 2 02 3 4 131 2 1 01 3 2 12 3 4 10 Sample Output310 将已经建好的路的权值设为0 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <

python整合连续数字的练习,包含itertools\groupby用法

#汉字数字转阿拉伯数字 1 class ConvertNum: 2 def __init__(self,cnNum): 3 self.dict = {u'零':0,u'一':1,u'二':2,u'三':3,u'四':4,u'五':5,u'六':6,u'七':7,u'八':8,u'九':9,u'十':10,u'百':100,u'千':1000,u'万':10000} 4 self.cnNum = cnNum 5 6 def convert(self): 7 count = 0 8 result =

查找bad sql的方法:

1 --查找bad sql的方法: 2 3 select * from (select buffer_gets, sql_text 4 5 from v$sqlarea 6 7 where buffer_gets >500000 8 9 order by buffer_gets desc) where rownum<=30; 10 11 -- 执行次数多的SQL 12 13 select sql_text,executions from 14 15 (select sql_text,execu

对于DelayQueue的理解

今天看公司代码,发现里面使用了 DelayQueue,学习以后记录下来: 概念:DelayQueue是一个支持延时获取元素的无界阻塞队列.里面的元素全部都是“可延期”的元素,列头的元素是最先“到期”的元素,如果队列里面没有元素到期,是不能从列头获取元素的,哪怕有元素也不行.也就是说只有在延迟期到时才能够从队列中取元素. 我理解为:延迟队列用于需要延迟处理的场景:比如延迟会话关闭,如果某一会话1分钟后需要关闭,则可以使用延迟队列,再比如:订单超时取消 例子场景:订单超时 public class

吉萨法律时间浪费拉丝粉;阿里山

http://www.ebay.com/cln/tmotshu/book/157890749018/2015.01.27.html http://www.ebay.com/cln/tmotshu/book/157890755018/2015.01.27.html http://www.ebay.com/cln/tmotshu/book/157890757018/2015.01.27.html http://www.ebay.com/cln/tmotshu/book/157890775018/20