[C][代码实例]冒泡排序

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

void swap(const char**, const char**);
void bubble_sort(const char**, int);

int main(void)
{
	char str_1[] = {
		52,5,8,96,78,23,12,4,9,2
	};
	int str_len = sizeof(str_1);

	const char **pStr_1 = malloc(sizeof(char*)*str_len);
	int i = 0;
	for (; i < str_len; ++i)
	{
		*(pStr_1+i) = str_1+i;
	}
	bubble_sort(pStr_1, str_len);
	i = 0;
	for (; i < str_len; ++i)
	{
		printf("%d\n", *(*(pStr_1+i)));
	}
}

void swap(const char **p1, const char **p2)
{
	const char *pT = *p1;
	*p1 = *p2;
	*p2 = pT;
}

void bubble_sort(const char **arr, int len)
{
	int i = 0;
	int j = 0;
	for (; i < len; ++i)
	{
		
		j = len - 1;
	 	for (; j > i; --j)
		{
		 	if( *(*(arr+j)) < *(*(arr+(j-1))) )
		 	{
		 		swap(arr+j, arr+(j-1));
		 	}
		}
	} 
}

  

posted @ 2018-02-03 11:07  yiyide266  阅读(261)  评论(0编辑  收藏  举报