Write a Python Program to Sort an Odd-Even sort or Odd even transposition SortIn this tutorial, we will learn about the transposition sort, also known as the brick sort or parity sort. It is a relatively simple sorting algorithm, developed originally for use on parallel processors with local interconnections. This algorithm is similar to the bubble sort and shares many feature characteristics. It is designed to compare all odd/even indexed pairs of adjacent elements in the list, and if a pair is in the wrong order, the elements are swapped. This step is repeated for even/odd indexed. Then it alternates between odd/even and even/odd steps until the list is sorted. Let's have an example to understand it in a better way. Suppose we have a list of some integers. We need to arrange the numbers as even then odd. So we need to place the even number first and then the odd numbers. If the given list is a [3, 7, 8, 2, 1, 9, 4, 6], the result will be like [2, 4, 6, 8, 3, 7, 9]. We will use the following steps to solve this problem - set i := 0 and j := 0 while j < size of list o if list[j] is even, then swap list[i] and list[j], and increase i by 1 o increase j by 1 return list Let's implement the above solution into Python code - Example - Output - Original list: [4, 3, 5, 1, 2] Sorted order is: [1, 2, 3, 4, 5] Original list: [15, 29, 100, 23, -14, 45, 178, 192, 246, -118, 0, 7] Sorted order is: [-118, -14, 0, 7, 15, 23, 29, 45, 100, 178, 192, 246] Explanation - In the above code, we have created a function named parity sort, which takes a list as an argument. Into the function, we initialized the length of the list, and we used the nested for loop. The outer for loop will run to the length of the list, and the inner loop will run from modulus 2 to the end of the list and jump two elements of the list. If the next element is smaller than the previous element, then swap the element. Once the list is sorted, the function returns the sorted list. We can also solve this problem using the while loop. Let's understand the following example. Example - Output - Original list: [4, 3, 5, 1, 2] Sorted order is: [4, 2, 5, 1, 3] Original list: [15, 29, 100, 23, -14, 45, 178, 192, 246, -118, 0, 7] Sorted order is: [100, -14, 178, 192, 246, -118, 0, 23, 29, 45, 15, 7] ConclusionIn this tutorial, we have implemented the parity sort or even-odd sort. We can use for loop or while loop to solve this problem. This problem can be asked in the technical interviews. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India