日常编程笔记 | 2022.10.1 | 选择问题

奇怪的排序

忘记更新tmp了

在这里插入图片描述

在这里插入图片描述

C源码

//selection prolem O(N) and its test program
//select the Kth large
#include<stdio.h>
#include<stdlib.h>
#define N 10
#define K 3

int Select(int*a);
void BubbleSort(int* array, int n);
int* GeneArray();

int main(){
	int* array = GeneArray();
	int k = Select(array);
	int i;
	BubbleSort(array, N);
	for(i = 0; i < N; i++)
	{
		printf("%d ", array[i]);
	}
	printf("\n%d", k);

	return 0;	
} 

int Select(int*a)
{
	int*arr = (int*)malloc(K*sizeof(int));//malloc space to arr
	int end = K - 1;
//	int flag = 0;//to judge whether the new one is inserted 0-no 1-yes
	int tmp;//to store the present inserted number
	int i;
	for(i = 0; i < K; i++)//select K elements out of a
	{
		arr[i] = a[i];
	}
	
	tmp = a[K];//the first inserted one is a[K]
	BubbleSort(arr, K);//sort the arr in increasing order
	
	for(i = K; i < N; i++)
	{
		tmp = a[i];//inserted an element start from a[K]
		end = K - 1;//the "invisibal" boundary of sorted array is equal to the number of rounds
		while(end >= 0)
		{

			//I ignore the case that there are same numbers
			if(tmp < arr[end])//if the insert one is smaller than the biggest sorted element
			{
//				flag = 1;
				if(end == K - 1);//special treat goes to the last element of arr
				else
					arr[end + 1] = arr[end];//insert sort
				end--;
			}
			else 
				break;
		}
//		if(flag)
		if(end < K - 1)
			arr[end + 1] = tmp;//there are two cases that the program runs here:
//			flag = 0;		   //1. end is below 0, the new one is attached to the front of the sorted array
							   //2. the value of tmp is between arr[0] and arr[end] 
	}
	
	return arr[K - 1];
	
}

void BubbleSort(int* array, int n)
{
	int isSorted = 1;//to decide wether the array is ordered
	int SortedIndex = n - 1;//to record the left sorted index
	int i,j;
	int temp;
	int thisIndex;
		
	for(i = 0; i < n; i++)//control the loop times
	{
		isSorted = 1;//initialize the state
		for(j = 0; j < SortedIndex; j++)//SortedIndex is the right boundary of sorted elements
										//which can spare you of a lot trouble of sorting ordered array 
		{
			if(array[j] > array[j + 1])
			{
				isSorted = 0;//the array is not in order yet
				temp = array[j];//exchange the order
				array[j] = array[j + 1];
				array[j + 1] = temp;
				thisIndex = j;
			}
					
		}
		if(isSorted)
			break; //if the array is already in order, then stop the loop
		SortedIndex = thisIndex;//renew the right boundary of sorted elements
				
	}
			
}


int* GeneArray()
{
	srand((unsigned int)time(0));//initialize the seed
	
	int i;
	int*array = (int*)malloc(sizeof(int)*N);
	
	for(i = 0; i < N; i++)
	{
		array[i] = rand()%200 - 100;//to generate random number from -100 to 100 
	}
	
	return array;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Burp Suite 2022.6.1是Burp Suite的一个版本,它是一个用于Web应用程序渗透测试的工具。根据引用,您可以通过关注VX公众号401SecNote并回复"burp"来获取Burp Suite Professional v2022.6.1及其运行环境。此外,根据引用,您还可以通过百度网盘链接https://sysin.org/blog/burp-suite-pro-2022-6/下载Burp Suite Professional / Community 2022.6版本。如果您正在考虑安装高版本的Burp Suite,但担心与其他软件的兼容性问题,根据引用,您可以使用高版本的jdk来解决兼容性问题,并记录解决过程以备将来参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Burp Suite Professional 2022.6 (macOS, Linux, Windows) - Web 应用安全、测试和扫描](https://blog.csdn.net/netgc/article/details/125592616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [笔记 | 在JAVA1.8环境下安装高版本Burp Suite Pro](https://blog.csdn.net/dust_hk/article/details/126489797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值