|
-
December 3rd, 2009, 08:13 PM
#1
please trim my code of quick sort. thank you.
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
int a[] = {100,2,5,6,7,9,13,45,55};
template<class T>
void display(T *a, int n)
{
for (int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
template<class T>
void swap(T *a, T *b)
{
T tmp = *a;
*a = *b;
*b = tmp;
}
template<class T>
void quickSort(T *p_s, T *p_e)
{
if ( p_s == p_e ) return;
T *p_s_tmp , *p_e_tmp;
p_s_tmp = p_s;
p_e_tmp = p_e;
int *p_m;
int d = (p_e-p_s)/2;
p_m = p_s +d;
int pivot = *p_m;
while ( p_s < p_e )
{
while ( pivot < *p_e )
p_e--;
while ( *p_s < pivot )
p_s++;
swap( p_e,p_s );
display<int>(a, 9);
}
p_m = p_e;
if ( p_s_tmp < p_m )
{
T * p_s_tmp_p, * p_m_p;
p_s_tmp_p = p_s_tmp;
p_m_p = p_m;
quickSort(p_s_tmp_p,p_m_p);
}
if ( p_m < p_e_tmp )
{
T * p_e_tmp_p, * p_m_p;
p_e_tmp_p = p_e_tmp;
p_m_p = p_m + 1;
quickSort(p_m_p,p_e_tmp_p);
}
}
int _tmain(int argc, TCHAR *argv[])
{
quickSort(a,a+8);
}
-
December 3rd, 2009, 09:09 PM
#2
Re: please trim my code of quick sort. thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|