• [C][代码实例]整型数组二分排序


    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    
    void _qsort(void*, size_t, size_t);
    void vswap(void*, void*, size_t);
    
    int main(void)
    {
    	int int_1[] = {
    		85,9,32,64,12,7,9,51,2,63
    	};
    	size_t len = sizeof(int);
    	/*char int_1[] = {
    		85,9,32,64,12,7,9,51,2,63
    	};
    	size_t len = sizeof(char);*/
    
    
    	size_t count = sizeof(int_1)/len;
    	void *p_1 = malloc(count*len);
    	memcpy(p_1, int_1, count*len);
    
    	_qsort(p_1, count, len);
    	printf("ALL Done-----------------
    ");
    
    	size_t total_len = len * count;
    	int i = 0;
    	for (; i < total_len; ++i)
    	{
    		if(i%len != 0)
    		{
    			continue;
    		}
    		printf("%d,", *((int *)(p_1+i)));
    		//printf("%d
    ", *((char *)(p_1+i)));
    	}
    	printf("
    ");
    
    }
    void _qsort(void *p,  size_t count,  size_t len)
    {
    	if(count == 2)
    	{
    		if( *((int*)(p)) > *((int*)(p+len)) )
    		{
    			vswap(p, p+len, len);
    		}
    	}
    	if(count > 2)
    	{
    		int mid_edge = (count + count%2)/2 - 1;
    		size_t total_len = len * count;
    		void *p_t = malloc(total_len);
    		int i = 0;
    		int index = 0;
    		int l_count = 0;
    		int r_count = 0;
    		for (; i < total_len; ++i)
    		{
    			if(i%len != 0)
    			{
    				continue;
    			}
    			if(i/len == mid_edge)
    			{
    				continue;
    			}
    			if( *((int*)(p+i)) <= *((int*)(p+(mid_edge*len))) )
    			{
    				memcpy(p_t+(index*len), p+i, len);
    				printf("l :%d, val :%d
    ", index, *((int*)(p+i)));
    				index++;
    				l_count++;
    			}
    		}
    		//set mid
    		memcpy(p_t+(index*len), p+(mid_edge*len), len);
    		printf("mid_edge :%d, val :%d
    ", index, *((int*)(p+(mid_edge*len))));
    		index++;
    		//set right
    		i = 0;
    		for (; i < total_len; ++i)
    		{
    			if(i%len != 0)
    			{
    				continue;
    			}
    			if(i/len == mid_edge)
    			{
    				continue;
    			}
    			if( *((int*)(p+i)) > *((int*)(p+(mid_edge*len))) )
    			{
    				memcpy(p_t+(index*len), p+i, len);
    				printf("r :%d, val :%d
    ", index, *((int*)(p+i)));
    				index++;
    				r_count++;
    			}
    		}
    
    		memcpy(p, p_t, total_len);
    		free(p_t);
    		//recursive
    		printf("%d
    ", l_count);
    		printf("%d
    ", r_count);
    		printf("Done-----------------
    ");
    		_qsort(p,  l_count, len);
    		_qsort(p+((l_count+1)*len),  r_count, len);
    	}
    }
    
    
    
    void vswap(void *p_1, void *p_2, size_t len)
    {
    	void *p_t = malloc(len);
    	memcpy(p_t, p_1, len);
    	memcpy(p_1, p_2, len);
    	memcpy(p_2, p_t, len);
    	free(p_t);
    }
    
  • 相关阅读:
    学习WWDC的好资源!
    运行 CMD 时,參数加引號常见问题
    FileChannel的深入理解
    C#单例模式的三种写法
    Linux 安装Nginx具体图解教程
    计网面试题
    VS:&quot;64位调试操作花费的时间比预期要长&quot;的一解决途径
    中小型WEB系统权限日志数据表设计
    CDN服务上线,DNSPOD布局云端生态圈
    怎样利用ash监控会话
  • 原文地址:https://www.cnblogs.com/yiyide266/p/8431980.html
Copyright © 2020-2023  润新知