A recursive version of Quicksort for sorting characters : Sort « Development « C++ Tutorial






#include <iostream> 
#include <cstring> 
 
using namespace std; 
 
void quicksort(char *items, int len); 
 
void qs(char *items, int left, int right); 
 
int main() { 
 
  char str[] = "fdsawertgsvbnhgfdertygfd"; 
 
  cout << "Original order: " << str << "\n"; 
 
  quicksort(str, strlen(str)); 
 
  cout << "Sorted order: " << str << "\n"; 
 
  return 0; 
 
} 
 
void quicksort(char *items, int len) 
{ 
  qs(items, 0, len-1); 
} 
 
void qs(char *items, int left, int right)  
{  
  int i, j;  
  char x, y;  
  
  i = left; j = right;  
  x = items[( left+right) / 2 ];  
  
  do {  
    while((items[i] < x) && (i < right)) 
       i++;  
    while((x < items[j]) && (j > left)) 
       j--;  
  
    if(i <= j) {  
      y = items[i];  
      items[i] = items[j];  
      items[j] = y;  
      i++; j--;  
    }  
  } while(i <= j);  
  
  if(left < j) 
     qs(items, left, j);  
  if(i < right) 
     qs(items, i, right);  
}
Original order: fdsawertgsvbnhgfdertygfd
Sorted order: abdddeefffggghnrrssttvwy








5.22.Sort
5.22.1.A Bubble sort
5.22.2.A recursive version of Quicksort for sorting characters
5.22.3.Quick Sort
5.22.4.how to declare your own function and function pointer to be used with qsort( )
5.22.5.Sort Tracer
5.22.6.Selection Sort