IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> C++选择,插入,希尔排序算法 -> 正文阅读

[数据结构与算法]C++选择,插入,希尔排序算法

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#define NUM 100000

void print(int* a,int len,bool is=true);

//选择排序
void select_sort(int* a,int len);
//插入排序
void insert_sort(int* a, int len);
//希尔排序
void shell_sort(int* a, int len);

void init(int* a);

int _tmain(int argc, _TCHAR* argv[])
{
	int arr[] = { 9, 0, 5, 6, 4, 7, 8, 1, 3, 2 };
	int a[NUM];
#if NUM==10
	print(arr,NUM);

	shell_sort(arr, NUM);

	print(arr, NUM, 0);
#else
	init(a);//初始化数组

	clock_t oldTime, newTime;

	oldTime = clock();
	insert_sort(a, NUM);
	newTime = clock();
	printf("%d\n", newTime - oldTime);


	oldTime = clock();
	select_sort(a, NUM);
	newTime = clock();
	printf("%d\n", newTime - oldTime);

	oldTime = clock();
	shell_sort(a, NUM);
	newTime = clock();
	printf("%d\n", newTime - oldTime);


#endif




	while (1);
	return 0;
}

void init(int* a){
	srand(time(NULL));
	for (int i = 0; i < NUM; i++)
		a[i] = rand() % NUM;
}


void print(int* a, int len, bool is){
	if (is==0){
		printf("after  sort:");
	}
	if (is > 0){
		printf("before sort:");
	}

	for (int i = 0; i < len; i++)
		printf("%d ", a[i]);

	printf("\n");
}

//选择排序
void select_sort(int* a, int len){
	printf("选择排序!\n");

	//总共len-1次
	int minIdx;
	int temp;
	for (int i = 0; i < len - 1; i++){
		//1 从  a[i]  到  a[len - 1] 选出最小的
		minIdx = i;
		for (int j = i; j < len; j++){
			//if (a[minIdx] > a[j]){minIdx = j;}
			minIdx = (a[minIdx]<a[j]) ? minIdx : j;
		}
		//2 和 a[i]交换
		if (minIdx != i){
			temp = a[minIdx];
			a[minIdx] = a[i];
			a[i] = temp;
		}
		//print(a, NUM,-1);
	}



}

//插入排序
void insert_sort(int* a, int len){
	printf("插入排序!\n");
	int temp;
	int idx;//待插位置
	//插入len-1次   第 n 次插入下标为 n 的数据
	for (int i = 1; i < len; i++){
		//1 临时存储待插数据
		temp = a[i];
		//2 定位要插入位置,并且把要插入位置之后的数据后移
		
		/*
		idx = i;
		while (idx >= 0){//循环之后  idx就是要插入位置
			if (a[idx]>=temp)
				idx--;
			else
				break;
		}
		for (int j = i-1; j > idx; j--)//把要插入位置之后的数据后移
			a[j + 1] = a[j];
		*/
		idx = i-1;
		while (idx >= 0 && a[idx] > temp){
			a[idx + 1] = a[idx];
			idx--;
		}
		//3 待插数据赋值到要插入位置上
		a[idx + 1] = temp;
		//print(a, len, -1);
	}


}

//希尔排序
void shell_sort(int* a, int len){
	printf("希尔排序!\n");

	int step = (len >> 1);
	int temp;
	int idx;//待插位置
	while (step >= 1){
		for (int i = step; i < len; i++){
			//1 临时存储待插数据
			temp = a[i];
			//2 定位要插入位置,并且把要插入位置之后的数据后移
			idx = i - step;
			while (idx >= 0 && a[idx] > temp){
				a[idx + step] = a[idx];
				idx-=step;
			}
			//3 待插数据赋值到要插入位置上
			a[idx + step] = temp;
			
		}


		step >>= 1;//步长变为原来的一半
		//print(a, len, -1);
	}


}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-17 12:12:08  更:2021-07-17 12:12:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 4:41:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码