标签:data fine mil 冒泡排序 线性时间 lse 10个 iostream art
十种常见排序算法可以分为两大类:
摘自https://www.cnblogs.com/onepixel/articles/7674659.html
如下只有冒泡和桶排序
/*冒泡算法测试*/
/*产生随机整数--int32 10个*/
/*排序*/
/*检测排序结果*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
#include <iostream>
using namespace std;
#define uint unsigned int
#define int64_t long long
#define NUM_R 10
#define TONG 255
/*打印排序结果*/
void print_buf(uint num, int *buf)
{
uint i;
printf("after data:");
for (i = 0; i < num; i++)
printf("%d ", *buf++);
printf("\n");
}
/*测试排序顺序*/
int test_seq(uint num, int *buff)
{
uint i;
for (i = 0; i < num-1; i++)
{
if (*buff <= *(buff + 1))
buff++;
else
return -1;
}
return 0;
}
/*冒泡排序*/
void swap_func(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void re_seq1(uint num, int *buff)
{
uint i, j;
uint re_q = num - 1;
for (i = 0; i < re_q; i++)
{
for (j = 0; j < re_q - i; j++)
{
if (buff[j] > buff[j + 1])
swap_func(&buff[j], &buff[j + 1]);
else
;
}
}
}
/*桶排序*/
void TongSort(int num,int *score)
{
int a[TONG + 1] = {0};
for (int i = 0; i<num; i++)
{
int temp = score[i];
a[temp]++;
}
for (int i = 0; i<TONG; i++)
{
int num_print = a[i];
for (int j = 1; j <= num_print; j++)
{
*score = i;
score++;
}
}
}
int64_t getCurrentTime()
{
struct timeb rawtime;
ftime(&rawtime);
return rawtime.time * 1000 + rawtime.millitm;
}
void rand_m(uint num, int *buff)
{
int i = 0;
printf("随机数:");
for (; i < num; i++)
{
srand((uint)getCurrentTime());
*buff = rand() % TONG;
printf("%d ", *buff);
buff++;
Sleep(10);
}
printf("\n");
}
int main(int argc, char ** argv)
{
printf("******排序测试*******\n");
int buf[100] = { 0 };
int res = 0;
if (NUM_R > 100)
{
printf("err NUM_R \n");
return -1;
}
rand_m(NUM_R, buf);
re_seq1(NUM_R,buf);
//TongSort(NUM_R, buf);
res = test_seq(NUM_R, buf);
if (res < 0)
printf("\n****err*****\n");
print_buf(NUM_R, buf);
system("PAUSE");
}
标签:data fine mil 冒泡排序 线性时间 lse 10个 iostream art
原文地址:https://www.cnblogs.com/xiuyaa/p/13635992.html