快速排序的动态显示比较简单,不多说,直接上代码:
//快速排序动态显示 #include<stdio.h> #include <stdlib.h> #include <Windows.h> void swap(int arry[],int num1, int num2) { int temp = arry[num1]; arry[num1] = arry[num2]; arry[num2] = temp; } void show_arry(int arry[],int len) { int i =0; for(i = 0;i < len;i++) { printf( "%d ",arry[i]); } printf( "\n"); } void my_sqort(int arry[],int left, int right,int len) { int last; int i = 0; if(left >= right) { return; } swap(arry,left,(left+right)/2); last = left; for(i = left+1;i <= right;i++) { if(arry[i] < arry[left]) { swap(arry,i,++last); } } show_arry(arry,len); //for(i = 0;i<1000000000;i++) //{ // ; //} Sleep(2000); system( "cls"); swap(arry,left,last); my_sqort(arry,left,last-1,len); my_sqort(arry,last+1,right,len); } int main() { int arry[] = {11,5,23,45,77,9,556,23,11}; my_sqort(arry,0,( sizeof(arry)/sizeof (arry[0]))-1,sizeof(arry)/ sizeof(arry[0])); show_arry(arry, sizeof(arry)/sizeof (arry[0])); return 0; }
下面是字符串的快速排序:
1.注意在函数中比较字符串的操作需要运用二级指针进行操作,否则无法对指针的地址进行变换。
2.原理跟快速排序一个意思。
比较简单,只要注意函数字符串的操作有效性就OK:
代码:
#include <stdio.h> #include <string.h> #include <Windows.h> #include <stdlib.h> int compare(char **arry,int left, int right) { return strcmp(*(char **)(arry+left),*(char **)(arry+right)); } void swap(char **arry,int left, int right) { char *temp = *(arry+left); *(arry+left) = *(arry+right); *(arry+right) = temp; } void my_csqort(char **arry,int left, int right) { int last = 0; int i = 0; if(left >=right) { return; } swap(arry,left,(left+right)/2); last = left; for(i = left+1;i <= right;i++) { if(compare(arry,left,i) > 0) { swap(arry,i,++last); } } swap(arry,last,left); for(i = 0;i<5;i++) { printf( "%s\n",arry[i]); } Sleep(2000); system( "cls"); my_csqort(arry,left,last-1); my_csqort(arry,last+1,right); } int main() { int i = 0; char *arry[] = {"bbbbb" ,"aaaaa", "ccccc","xxxxx" ,"ddddd"}; my_csqort(arry,0, sizeof(arry)/sizeof (arry[0])-1); for(i = 0;i<5;i++) { printf( "%s\n",arry[i]); } printf( "%d",strcmp("xxxxx" ,"qqqqq")); return 0; }
本文出自 “剩蛋君” 博客,转载请与作者联系!
原文地址:http://memory73.blog.51cto.com/10530560/1703164