Program for Bogo Sort using Python

by | Jun 12, 2021 | Python Programs

Home » Python » Python Programs » Program for Bogo Sort using Python

Introduction

Bogo sort is based on generate and test paradigm and is also known as Permutation sort, shotgun sort or monkey sort. Our task is to sort the given array elements using bogo sort. It ineffective sorting algorithm.

Program

import random
def sort(arr):
    length = len(arr)
    while (sorted_ele(arr) == False):
        shuffle_ele(arr)
def sorted_ele(arr):
    length = len(arr)
    for i in range(0, length - 1):
        if (arr[i] > arr[i + 1]):
            return False
    return True
           
def shuffle_ele(arr):
    length = len(arr)
    for i in range(length):
        r = random.randint(0, length-1)
        arr[i], arr[r] = arr[r], arr[i]
ip_arr = [10, 2, 51, 3]
sort(ip_arr)
print("The sorted array is: ")
for i in range(0, len(ip_arr)):
    print(ip_arr[i])

Output

Program for Bogo Sort using Python

Explanation

In the above program, we have select one element from array and randomly place it at position. This process of sorting continues until all the items within the array is sorted.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author