0

I tried to modify bubble sort algorithm but it does not work correctly, it sorts the list arr2 like ['a', 'abc', 'bba', 'bbbcc', 'ccaakl', 'ccb', 'çalışkan çocuk ', 'çimen', 'mmmmmm', 'zmm', 'ğayrimenkul', 'şimşek', 'şş'] as you can see , letters "ğ","z","ş" are wrong sorted. I cannot find the mistake.

Here is the dict and arr:

arr2=["çalışkan çocuk ","a","şş","bbbcc","ccaakl","zmm","mmmmmm","abc","bba","ccb","şimşek","çimen","ğayrimenkul"]

letters_dict ={" ":30,"a": 0, "b": 1, "c": 2, "ç": 3, "d": 4, "e": 5, "f": 6, "g": 7, "ğ": 8, "h": 9, "ı": 10, "i": 11,"j": 12, "k": 13,
               "l": 14, "m": 15, "n": 16, "o": 17, "ö": 18, "p": 19, "r": 20, "s": 21, "ş": 22,"t": 23, "u": 24, "ü": 25, "v": 26, "y":27,"z":28}

And the sorting function:

def bubbleSort(arr,letters_dict):
   n = len(arr)

   for i in range(n - 1):
      for j in range(0, n - i - 1):
         if (("ç" or "ğ" or "ö" or "ş" or "ü") in arr[j]) or (("ç" or "ğ" or "ö" or "ş" or "ü") in arr[j+1]):
            for x in range(len(min(arr[j],arr[j+1]))):
               if letters_dict[arr[j][x]] > letters_dict[arr[j+1][x]]:
                  arr[j], arr[j + 1] = arr[j + 1], arr[j]
                  break
               elif letters_dict[arr[j][x]] < letters_dict[arr[j+1][x]]:
                  break
         else:
            if arr[j] > arr[j + 1]:
               arr[j], arr[j + 1] = arr[j + 1], arr[j]
   return arr
Azula
  • 9
  • 3
  • "it does not work correctly": Please provide details. – Scott Hunter Feb 05 '21 at 20:47
  • 1
    How do you know it doesn't work? – Peter Wood Feb 05 '21 at 20:48
  • 1
    There is a first serious misconception in your code: `"ç" or "ğ" or "ö" or "ş" or "ü"` actually returns `"ç"`. I mean, try `("ç" or "ğ" or "ö" or "ş" or "ü") == "ç"`. See [this](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) to go further. – keepAlive Feb 05 '21 at 20:52
  • See https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true – Thierry Lathuille Feb 05 '21 at 20:53
  • You cannot just sort these by their Unicode codepoint. The rules on how to sort special characters even differ from language to language. You can access this through the locale module in python. – MaxNoe Feb 05 '21 at 20:53

0 Answers0