一个新的算法:珠排序

来源:互联网 发布:淘宝贷款怎么回事 编辑:程序博客网 时间:2024/05/23 11:46

其实在c++里面,有很多鲜为人知的算法。比如说这个,珠排序,就是我翻了很久才找到的。
珠排序到底是什么呢?其实原理跟他的名字一样简单,大概是这样的:
首先,你要找到一个算盘
上面有几根柱子,可以放一些珠子。
输入呢就是有几就那一行有几个珠子。
然后呢类似于一个把这个算盘用力一震,使所有珠子子落下。
最后得到的就是上面小下面大的有序排列。
代码:

#include <stdio.h>#include <ctype.h>int temp1,temp2;int main(){int times = 2;int counter = 0;int i = 0;int j = 0;printf("How many numbers do you want to add in? ");do{printf("Please add in a number:");scanf("%d",&times);}while(times <= 0);printf("\n");int numbers[times];for (int k = 0;k < times;k++){printf("Please enter number %d :",k+1);scanf("%d",&numbers[k]);}/************************Find the largest number*************************/int max = numbers[0];for (int h = 0;h < times;h++){if(max < numbers[h]){max = numbers[h];}} /**************************Sort the numbers[1]***************************/int temp3 = 0;int numbers1[max];do{temp3 = 0;for(j = 0;j < times;j++){if(numbers[j] < i + 1){temp3++;}}numbers1[i] = temp3;i++;}while(i < max);/**************************Print the numbers[1]**************************/printf("\nHere is the numbers after the first sorting: ");for (int k = 0;k < i;k++){printf("%d , ",numbers1[k]);}printf("\b\b \n");/**************************Sort the numbers[2]***************************/int numbers2[times];int temp4 = 0;int n = 0;for(int l = 0 ;l < times;l++){for(n = 0;n < i;n++){if(numbers1[n] < l + 1){temp4++;}}numbers2[l] = temp4;temp4 = 0;}/**************************Print the numbers[2]**************************/printf("\nHere is the numbers after the second sorting: ");for (int m = 0;m < times;m++){printf("%d , ",numbers2[m]);}printf("\b\b \n");/****************************Restart or End******************************/printf("\n\n\nRestart or End?\n");char RorE;do{printf("Please enter \"R\"or\"E\". ");scanf(" %c",&RorE);if(RorE == 0){continue;}RorE = toupper(RorE);}while(RorE != 'R' && RorE != 'E');if(RorE=='R'){printf("\n\nRestarting...\n\n\n");main();}}

好吧,到这