主题:非递归的快速排序问题
一个字符串数组,
abd20ef
cf3084lg
egi66nnnpk
gop52896 hi
pfr122f
ope481pk
adc681p6
vbg32 4lo
asd34f
bgh61lo
bgh20pol,
fgh269g
要求对每个字符串的4、5位进行排序,然后输出排序后的字符串,用非递归的快速排序实现。代码如下:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//#include <malloc.h>
#define LENGTH 12
void swap(int array[], int low, int high,char str[][100])
{
int temp;
char tempa[100];
temp = array[low];
array[low] = array[high];
array[high] = temp;
strcpy(tempa,str[low]);
strcpy(str[low],str[high]);
strcpy(str[high],tempa);
}
int partition(int array[], int low, int high,char str[][100])
{
int p;
p = array[low];
while (low < high)
{
while (low < high && array[high] >= p)
high--;
swap(array,low,high,str);
while (low < high && array[low] <= p)
low++;
swap(array,low,high,str);
}
return low;
}
//
// void qsort_recursive(int array[], int low, int high,char str[][100])//递归
// {
// int p;
//
// if(low < high)
// {
// p = partition(array, low, high,str);
// qsort_recursive(array, low, p - 1,str);
// qsort_recursive(array, p + 1, high,str);
// }
// }
void qsort_nonrecursive(int array[], int low, int high,char str[][100])//非递归
{
int m[50], n[50], cp, p;
/* 初始化栈和栈顶指针 */
cp = 0;
m[0] = low;
n[0] = high;
while (m[cp] < n[cp])
{
while (m[cp] < n[cp])
{ /* 向左走到尽头 */
p = partition(array, m[cp], n[cp],str); /* 对当前结点的访问 */
cp++;
m[cp] = m[cp - 1];
n[cp] = p - 1;
}
/* 向右走一步 */
m[cp + 1] = n[cp] + 2;
n[cp + 1] = n[cp - 1];
cp++;
}
}
int main()
{
int i,n;
char str[LENGTH][100],b[LENGTH][100];
int shuju[LENGTH];
char strr[100];
FILE *fi;
fi=fopen("test1.txt","r");
if(fi==NULL)
{
printf("can't open test.txt to read!\n");
exit(0);
}
for(i=0;i<LENGTH;i++)
{
fgets(strr,100,fi);
strcpy((char*)str[i],strr);
}
fclose(fi);
for(i=0;i<LENGTH;i++)
{
printf("%s\n",str[i]);
}
for(i=0;i<LENGTH;i++)
{
strncpy(b[i],str[i]+3,2);
b[i][5]=0;
}
for(i=0;i<LENGTH;i++)
{
shuju[i]=atoi((char*)b[i]);
}
qsort_nonrecursive(shuju, 0, LENGTH-1,str);
printf("排序后:\n");
for(i=0;i<LENGTH;i++)
{
printf("%s\n ",str[i]);
// // printf("%s\n ",str[i]);
}
}
abd20ef
cf3084lg
egi66nnnpk
gop52896 hi
pfr122f
ope481pk
adc681p6
vbg32 4lo
asd34f
bgh61lo
bgh20pol,
fgh269g
要求对每个字符串的4、5位进行排序,然后输出排序后的字符串,用非递归的快速排序实现。代码如下:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//#include <malloc.h>
#define LENGTH 12
void swap(int array[], int low, int high,char str[][100])
{
int temp;
char tempa[100];
temp = array[low];
array[low] = array[high];
array[high] = temp;
strcpy(tempa,str[low]);
strcpy(str[low],str[high]);
strcpy(str[high],tempa);
}
int partition(int array[], int low, int high,char str[][100])
{
int p;
p = array[low];
while (low < high)
{
while (low < high && array[high] >= p)
high--;
swap(array,low,high,str);
while (low < high && array[low] <= p)
low++;
swap(array,low,high,str);
}
return low;
}
//
// void qsort_recursive(int array[], int low, int high,char str[][100])//递归
// {
// int p;
//
// if(low < high)
// {
// p = partition(array, low, high,str);
// qsort_recursive(array, low, p - 1,str);
// qsort_recursive(array, p + 1, high,str);
// }
// }
void qsort_nonrecursive(int array[], int low, int high,char str[][100])//非递归
{
int m[50], n[50], cp, p;
/* 初始化栈和栈顶指针 */
cp = 0;
m[0] = low;
n[0] = high;
while (m[cp] < n[cp])
{
while (m[cp] < n[cp])
{ /* 向左走到尽头 */
p = partition(array, m[cp], n[cp],str); /* 对当前结点的访问 */
cp++;
m[cp] = m[cp - 1];
n[cp] = p - 1;
}
/* 向右走一步 */
m[cp + 1] = n[cp] + 2;
n[cp + 1] = n[cp - 1];
cp++;
}
}
int main()
{
int i,n;
char str[LENGTH][100],b[LENGTH][100];
int shuju[LENGTH];
char strr[100];
FILE *fi;
fi=fopen("test1.txt","r");
if(fi==NULL)
{
printf("can't open test.txt to read!\n");
exit(0);
}
for(i=0;i<LENGTH;i++)
{
fgets(strr,100,fi);
strcpy((char*)str[i],strr);
}
fclose(fi);
for(i=0;i<LENGTH;i++)
{
printf("%s\n",str[i]);
}
for(i=0;i<LENGTH;i++)
{
strncpy(b[i],str[i]+3,2);
b[i][5]=0;
}
for(i=0;i<LENGTH;i++)
{
shuju[i]=atoi((char*)b[i]);
}
qsort_nonrecursive(shuju, 0, LENGTH-1,str);
printf("排序后:\n");
for(i=0;i<LENGTH;i++)
{
printf("%s\n ",str[i]);
// // printf("%s\n ",str[i]);
}
}