write a python program to store first year percentage of students in array write function for sorting array of floating point numbers in ascending order using quick sort and display top five scores

Introduction:

In the realm of sorting algorithms, quick sort stands tall as a formidable contender, renowned for its efficiency and prowess. Its divide-and-conquer approach has captivated programmers for decades, proving its worth in countless applications. Today, we embark on a journey into the depths of quick sort, armed with the mighty Python language.

Accepting length and array of floating-point numbers:

Our quest begins with the acceptance of the length and array of floating-point numbers. Utilizing the input() function, we empower the user to provide the necessary input, granting them control over the sorting process.

Python
def accept_input():
  """Accepts length and array of floating-point numbers from the user."""

  length = int(input("Enter the number of students: "))

  array = []
  for i in range(length):
    array.append(float(input("Enter student #{} percentage: ".format(i + 1))))

  return length, array

Implementing quick sort:

With the input gathered, we delve into the intricacies of quick sort. The algorithm works by partitioning the array around a pivot element, such that all elements smaller than the pivot are placed to its left, and all elements greater than or equal to the pivot are placed to its right. This process is then recursively applied to the resulting subarrays, until the entire array is sorted.

Python
def quick_sort(array, low, high):
  """Sorts the given array using quick sort."""

  if low >= high:
    return

  pivot = array[high]
  i = low - 1

  for j in range(low, high):
    if array[j] < pivot:
      i += 1
      array[i], array[j] = array[j], array[i]

  i += 1
  array[i], array[high] = array[high], array[i]

  quick_sort(array, low, i - 1)
  quick_sort(array, i + 1, high)

Returning the sorted array:

Once the sorting process is complete, the sorted array is returned, ready to be utilized for further computations or displayed to the user.

Python
def get_sorted_array(length, array):
  """Returns the sorted array."""

  quick_sort(array, 0, length - 1)

  return array

Printing to top five floating-point numbers:

To conclude our journey, we print the top five elements of the sorted array to five floating-point numbers. This provides a glimpse into the results of the sorting process, allowing the user to verify its accuracy.

Python
def print_sorted_array(sorted_array):
  """Prints the top five elements of the sorted array to five floating-point numbers."""
top5 = sorted_array[::-1]
for i in range(5):
print("{:.3f}".format(top5[i]))

Putting it all together:

Now that we have examined the individual components of our program, let us bring them together to form a cohesive whole.

Python
def main():
  """Main program."""

  length, array = accept_input()
  sorted_array = get_sorted_array(length, array)
  print_sorted_array(sorted_array)

if __name__ == "__main__":
  main()

Conclusion:

Our journey into the realm of quick sort has come to an end. We have explored the inner workings of this powerful algorithm, gaining a deeper understanding of its divide-and-conquer approach. Armed with this knowledge, we can now confidently tackle more complex sorting problems, knowing that quick sort stands ready to assist us in our endeavors.

 

Additional remarks:

  • The efficiency of quick sort is heavily influenced by the choice of pivot element. A well-chosen pivot can significantly reduce the number of comparisons required, resulting in a faster sorting process.
  • Quick sort is not a stable sorting algorithm, meaning that it does not preserve the relative order of equal elements. This can be a drawback in certain applications where the order of elements is important.
  • There are a number of variations of quick sort, each with its own advantages and disadvantages. Some of the most common variations include median-of-three quick sort, randomized quick sort, and introspective sort.

Post a Comment

0 Comments