top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Sorting a list of strings without using any built-in sort method

+4 votes
365 views

How can we perform Sorting a list of strings without using any built-in sort method?

posted Apr 18, 2014 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))

int q_sort(char *a[],int low,int high)
{
   int pivot,j,i;
   char *temp;
   if(low<high)
   {
      pivot=low;
      i=low;
      j=high;
      while(i<j)
      {
         while(strcmp(a[i],a[pivot])!=1 && i<high)
            i++;
         while(strcmp(a[j],a[pivot])==1)
            j--;
         if(i<j)
         {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
         }
      }
      temp=a[pivot];
      a[pivot]=a[j];
      a[j]=temp;
      q_sort(a,low,j-1);
      q_sort(a,j+1,high);
   }
}

void main()
{
   char *arr[]={"abc","def","ahcd"};
   int i;
      q_sort(arr,0,SIZEOF(arr)-1);
   for(i=0;i<SIZEOF(arr);i++)
   {
      printf("%s\n",arr[i]);
   }
}

Here, I have used quick sort algorithm to sort strings.

answer Sep 22, 2014 by Aarti Jain
Similar Questions
+4 votes

Requirements:

1.No input should be processed, and the output should be in the form of 2 3 5 7 11 13 ... etc.
2. No reserved words in the language are used at all
3.The language should at least allow structured programming, and have reserved words (otherwise point 2 would be moot).

...