Sort by length with stability in c

I am working on a coding problem when I have to sort an array of words (pointers to strings) by word length. I have an idea for code that uses some manipulation of indexes, but would like to help with checking the logic to make sure I'm right. Here is the code I have.

void len_sort(char** words, int num_words)
{
    int index=0;
    int isSorted=0;
    int string1_len;
    int string2_len;
    while(isSorted==0)
    {
        for(index=0;index<num_words;index++)
        {
            printf("%s\n", words[index]);
            string1_len=strlen(words[index]);
            if((index+1)==num_words)
                string2_len=strlen(words[index]);
            else
                string2_len=strlen(words[index+1]);

            if(string1_len<string2_len)
            {
                swap(words[index], words[index+1]);
            }
        }

        isSorted=1;

        for(index=0;index<num_words;index++)
        {
            string1_len=strlen(words[index]);
            if(index+1==num_words)
                string2_len=strlen(words[index]);
            else
                 string2_len=strlen(words[index+1]);
            if(string1_len>string2_len)
            {
                isSorted=0;
            }
        }

    }


}
void swap(char* word1, char* word2)
{

    char* temp;
    word1=word2;
    word2=temp;

}

I do not need to sort in alphabetical order, and I need to maintain the order of words as they are in the array. for example: if I have something like

car
x
horse
a

my output should look like this:

x
a
car
horse

preserving the fact that x to a in the array is true. Is there a better way to work with this type for greater efficiency?

+4
source share
3

. , :

void swap(char **word1, char **word2)
{
    char *temp;

    temp = *word1;
    *word1 = *word2;
    *word2 = temp;

}

, , , , . , .

void len_sort(char** words, int num_words)
{
    int i = 0; //start from the beginning.
    while(i < num_words-1) //till the last pair.
    {
        if(strlen(words[i]) > strlen(words[i+1])){//if a mismatch occur
            swap(&words[i], &words[i+1]);//fix it
            i = -1;//and start from the beginning 
        }
        i++;//so far sorted, try next pair
    }
}

:

int main()
{
    char d[][30] = {"car", "x", "horse", "a"};
    char *s[4];
    int i;

    //our function sort through pointer
    for(i=0; i<4; i++)
        s[i] = d[i];

    len_sort(s, 4);
    for(i=0; i<4; i++)
        printf("%s ", s[i]);
    puts("");

    return 0;
}

:

x a car horse
+1

. , , . .

:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int first;
  int second;
} pair;

int cmp(pair a, pair b){
  return (a.first > b.first) || ((a.first == b.first) && (a.second > b.second));
}

// you can use your favorite sort algorithm
// Since in some condition, quick_sort runs in O(n^2)
void quick_sort (pair *a, int n) {
    int i, j;
    pair p, t;
    if (n < 2)
        return;
    p = a[n / 2];
    for (i = 0, j = n - 1;; i++, j--) {
        while (cmp(a[i],p))
            i++;
        while (cmp(p,a[j]))
            j--;
        if (i >= j)
            break;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    quick_sort(a, i);
    quick_sort(a + i, n - i);
}

char** len_sort(char** words, int num_words){
  pair my_pairs[num_words];

  // iterate in O(n)
  for (int i = 0; i < num_words; ++i) {
    my_pairs[i].first = strlen(words[i]);
    my_pairs[i].second = i;
  }

  // sort in O(nlogn)
  quick_sort(my_pairs, num_words);

  // iterate through sorted and create ne wlist of words in O(n)
  char** new_word_list = (char**) malloc(num_words*sizeof(char*));
  for (int i = 0; i < num_words; ++i)
    new_word_list[i] = words[my_pairs[i].second];

  return new_word_list;
}
0

, :

uint32_t key = (strlen(s) << 16) | line_no;
0

Source: https://habr.com/ru/post/1616987/


All Articles