Thread: Sorting strings in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Sorting strings in C

    So I got the sorting program to work when I set the array without any numbers. However when reading from a file which has numbers before the word it is not behaving the way I want it to.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
      
    
    
    int cmpstr(void* v1, void* v2) 
    { 
         
        char *a1 = *(char**)v1; 
        char *a2 = *(char**)v2; 
        return strcmp(a1, a2); 
    } 
      
    // function for comparing two strings 
    
    
    void swap(void* v1, void* v2, int size) 
    { 
        
        char buffer[size]; 
      
        
        memcpy(buffer, v1, size); 
        memcpy(v1, v2, size); 
        memcpy(v2, buffer, size); 
    } 
      
    
    
    void _qsort(void* v, int size, int left, int right, 
                          int (*comp)(void*, void*)) 
    { 
        void *vt, *v3; 
        int i, last, mid = (left + right) / 2; 
        if (left >= right) 
            return; 
      
        // casting void* to char* so that operations  
        // can be done. 
        void* vl = (char*)(v + (left * size)); 
        void* vr = (char*)(v + (mid * size)); 
        swap(vl, vr, size); 
        last = left; 
        for (i = left + 1; i <= right; i++) { 
      
            
            vt = (char*)(v + (i * size)); 
            if ((*comp)(vl, vt) > 0) { 
                ++last; 
                v3 = (char*)(v + (last * size)); 
                swap(vt, v3, size); 
            } 
        } 
        v3 = (char*)(v + (last * size)); 
        swap(vl, v3, size); 
        _qsort(v, size, left, last - 1, comp); 
        _qsort(v, size, last + 1, right, comp); 
    } 
      
    int main() 
    { 
         
        char* a[] = {"bbc", "xcd", "ede", "def", 
                "afg", "hello", "hmmm", "okay", "how", "welcome" }; 
      
         
        _qsort(a, sizeof(char*), 0, 8, (int (*)(void*, void*))(cmpstr)); 
        
        for (int i = 0; i < 10; i++) 
            printf("%s ", a[i]); 
        printf("\n"); 
      
        
    }
    The desired output for the input is shown.
    Sorting strings in C-screenshot-201-png
    Sorting strings in C-screenshot-202-png
    Last edited by Timmy10; 12-18-2020 at 08:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Strings
    By tsmith94 in forum C Programming
    Replies: 0
    Last Post: 11-29-2011, 09:47 PM
  2. sorting strings
    By sid13 in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2011, 06:58 PM
  3. Sorting strings
    By yacek in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 02:07 PM
  4. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  5. Sorting Strings
    By SteelCityKid in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 01:14 PM

Tags for this Thread