Bogo Sort

Here given code implementation process.

// C Program 
// Sort arr elements by using Bogo Sort
#include <stdio.h>

#include <time.h>

#include <stdlib.h>

//Function which is display arr elements
void display(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
//This are check that whether given array is sorted or not
int is_sorted(int arr[], int size)
{
	for (int i = 0; i < size - 1; i++)
	{
		if (arr[i] > arr[i + 1])
		{
			//When array is unsorted
			return 0;
		}
	}
	//When array is sorted
	return 1;
}
//swapping the array elements in given location
void swap_data(int arr[], int i, int j)
{
	int temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
}
//shuffle array elements
void shuffle(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		swap_data(arr, i, rand() % (size));
	}
}
//Perform the bogosort in given array
void bogo_sort(int arr[], int size)
{
	//Check given array is sorted or not
	//Loop executes until the array permutation unsorted
	while (!is_sorted(arr, size))
	{
		//When array need to sort
		shuffle(arr, size);
	}
}
int main()
{
	//impotant sets the seed based on the current time
	srand(time(NULL));
	//Define an sorted arr of integers
	int arr[] = {
		11,
		8,
		3,
		8,
		32,
		-3,
		1,
		4,
		0,
		5
	};
	//Get the size of arr
	int size = sizeof(arr) / sizeof(arr[0]);
	//Before sort
	printf("\n  Before Sort  :\n");
	display(arr, size);
	//Sort element
	bogo_sort(arr, size);
	//After sort
	printf("\n  After Sort  :\n");
	display(arr, size);
	return 0;
}

Output

  Before Sort  :
  11  8  3  8  32  -3  1  4  0  5

  After Sort  :
  -3  0  1  3  4  5  8  8  11  32
/*
	Java Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	//This are check that whether given array is sorted or not
	public boolean is_sorted(int[] arr, int size)
	{
		for (int i = 0; i < size - 1; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				//When array is unsorted
				return false;
			}
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	public void swap_data(int[] arr, int i, int j)
	{
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//shuffle array elements
	public void shuffle(int[] arr, int size)
	{
		for (int i = 0; i < size; i++)
		{
			swap_data(arr, i, (int)(Math.random() * i));
		}
	}
	//Perform the bogosort in given array
	public void bogo_sort(int[] arr, int size)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!is_sorted(arr, size))
		{
			//When array need to sort
			shuffle(arr, size);
		}
	}
	public static void main(String[] args)
	{
		MySort obj = new MySort();
		//Define an sorted array of integers
		int[] arr = {
			11,
			8,
			3,
			8,
			32,
			-3,
			1,
			4,
			0,
			5
		};
		//Get the size of array
		int size = arr.length;
		System.out.print("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.bogo_sort(arr, size);
		System.out.print("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
//Include header file
#include <iostream>

#include <time.h>
#include <stdlib.h>


using namespace std;
/*
	C++ Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	public:
		//Function which is display arr elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	//This are check that whether given array is sorted or not
	bool is_sorted(int arr[], int size)
	{
		for (int i = 0; i < size - 1; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				//When array is unsorted
				return false;
			}
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	void swap_data(int arr[], int i, int j)
	{
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//shuffle array elements
	void shuffle(int arr[], int size)
	{
		for (int i = 0; i < size; i++)
		{
			this->swap_data(arr, i, rand()%(size));
		}
	}
	//Perform the bogosort in given array
	void bogo_sort(int arr[], int size)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!this->is_sorted(arr, size))
		{
			//When array need to sort
			this->shuffle(arr, size);
		}
	}
};
int main()
{
    //impotant sets the seed based on the current time
    srand(time(NULL));  
	MySort obj = MySort();
	int arr[] = {
		11 , 8 , 3 , 8 , 32 , -3 , 1 , 4 , 0 , 5
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << "\n Before Sort :\n";
	obj.display(arr, size);
	//Sort element
	obj.bogo_sort(arr, size);
	cout << "\n After Sort :\n";
	obj.display(arr, size);
	return 0;
}

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
//Include namespace system
using System;
/*
	C# Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	//This are check that whether given array is sorted or not
	public Boolean is_sorted(int[] arr, int size)
	{
		for (int i = 0; i < size - 1; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				//When array is unsorted
				return false;
			}
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	public void swap_data(int[] arr, int i, int j)
	{
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//shuffle array elements
	public void shuffle(int[] arr, int size)
	{
		Random location = new Random();
		for (int i = 0; i < size; i++)
		{
			swap_data(arr, i, location.Next(0, size));
		}
	}
	//Perform the bogosort in given array
	public void bogo_sort(int[] arr, int size)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!is_sorted(arr, size))
		{
			//When array need to sort
			shuffle(arr, size);
		}
	}
	public static void Main(String[] args)
	{
		MySort obj = new MySort();
		int[] arr = {
			11,
			8,
			3,
			8,
			32,
			-3,
			1,
			4,
			0,
			5
		};
		//Get the size of array
		int size = arr.Length;
		Console.Write("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.bogo_sort(arr, size);
		Console.Write("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
<?php
/*
	Php Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	//This are check that whether given array is sorted or not
	public	function is_sorted( & $arr, $size)
	{
		for ($i = 0; $i < $size - 1; $i++)
		{
			if ($arr[$i] > $arr[$i + 1])
			{
				//When array is unsorted
				return false;
			}
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	public	function swap_data( & $arr, $i, $j)
	{
		$temp = $arr[$i];
		$arr[$i] = $arr[$j];
		$arr[$j] = $temp;
	}
	//shuffle array elements
	public	function shuffle( & $arr, $size)
	{
		for ($i = 0; $i < $size; $i++)
		{
			$this->swap_data($arr, $i, rand() % $size);
		}
	}
	//Perform the bogosort in given array
	public	function bogo_sort( & $arr, $size)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!$this->is_sorted($arr, $size))
		{
			//When array need to sort
			$this->shuffle($arr, $size);
		}
	}
}

function main()
{
	$obj = new MySort();
	//Define an sorted array of integers
	$arr = array(11, 8, 3, 8, 32, -3, 1, 4, 0, 5);
	//Get the size of array
	$size = count($arr);
	echo "\n Before Sort :\n";
	$obj->display($arr, $size);
	//Sort element
	$obj->bogo_sort($arr, $size);
	echo "\n After Sort :\n";
	$obj->display($arr, $size);
}
main();

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
/*
	Node Js Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	//This are check that whether given array is sorted or not
	is_sorted(arr, size)
	{
		for (var i = 0; i < size - 1; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				//When array is unsorted
				return false;
			}
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	swap_data(arr, i, j)
	{
		var temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//shuffle array elements
	shuffle(arr, size)
	{
		for (var i = 0; i < size; i++)
		{
			this.swap_data(arr, i, Math.floor(Math.random() * (size - i) + i));
		}
	}
	//Perform the bogosort in given array
	bogo_sort(arr, size)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!this.is_sorted(arr, size))
		{
			//When array need to sort
			this.shuffle(arr, size);
		}
	}
}

function main()
{
	var obj = new MySort();
	//Define an sorted array of integers
	var arr = [11, 8, 3, 8, 32, -3, 1, 4, 0, 5];
	//Get the size of array
	var size = arr.length;
	process.stdout.write("\n Before Sort :\n");
	obj.display(arr, size);
	//Sort element
	obj.bogo_sort(arr, size);
	process.stdout.write("\n After Sort :\n");
	obj.display(arr, size);
}
main();

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
import random

#   Python 3 Program
#   Sort array elements by using Bogo Sort

class MySort :
    # Function which is display arr elements
    def display(self, arr, size) :
        i = 0
        while (i < size) :
            print(" ", arr[i], end = "")
            i += 1
        
        print("\n", end = "")
    
    # This are check that whether given array is sorted or not
    def is_sorted(self, arr, size) :
        i = 0
        while (i < size - 1) :
            if (arr[i] > arr[i + 1]) :
                # When array is unsorted
                return False
            
            i += 1
        
        # When array is sorted
        return True
    
    # swapping the array elements in given location
    def swap_data(self, arr, i, j) :
        temp = arr[i]
        arr[i] = arr[j]
        arr[j] = temp
    
    # shuffle array elements
    def shuffle(self, arr, size) :
        i = 0
        while (i < size) :
            self.swap_data(arr, i, random.randint(0,size-1))
            i += 1
        
    
    # Perform the bogosort in given array
    def bogo_sort(self, arr, size) :
        # Check given array is sorted or not
        # Loop executes until the array permutation unsorted
        while (not self.is_sorted(arr, size)) :
            # When array need to sort
            self.shuffle(arr, size)
        
    

def main() :
    obj = MySort()
    # Define an sorted array of integers
    arr = [11, 8, 3, 8, 32, -3, 1, 4, 0, 5]
    # Get the size of array
    size = len(arr)
    print("\n Before Sort :\n", end = "")
    obj.display(arr, size)
    # Sort element
    obj.bogo_sort(arr, size)
    print("\n After Sort :\n", end = "")
    obj.display(arr, size)

if __name__ == "__main__": main()

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
# 	Ruby Program
# 	Sort array elements by using Bogo Sort

class MySort

	# Function which is display arr elements
	def display(arr, size)
	
		i = 0
		while (i < size)
		
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
	# This are check that whether given array is sorted or not
	def is_sorted(arr, size)
	
		i = 0
		while (i < size - 1)
		
			if (arr[i] > arr[i + 1])
			
				# When array is unsorted
				return false
			end
			i += 1
		end
		# When array is sorted
		return true
	end
	# swapping the array elements in given location
	def swap_data(arr, i, j)
	
		temp = arr[i]
		arr[i] = arr[j]
		arr[j] = temp
	end
	# shuffle array elements
	def shuffle(arr, size)
	
		i = 0
		r = Random.new
		while (i < size)
		
			self.swap_data(arr, i, r.rand(0...size))
			i += 1
		end
	end
	# Perform the bogosort in given array
	def bogo_sort(arr, size)
	
		# Check given array is sorted or not
		# Loop executes until the array permutation unsorted
		while (!self.is_sorted(arr, size))
		
			# When array need to sort
			self.shuffle(arr, size)
		end
	end
end
def main()

	obj = MySort.new()
	# Define an sorted array of integers
	arr = [11, 8, 3, 8, 32, -3, 1, 4, 0, 5]
	# Get the size of array
	size = arr.length
	print("\n Before Sort :\n")
	obj.display(arr, size)
	# Sort element
	obj.bogo_sort(arr, size)
	print("\n After Sort :\n")
	obj.display(arr, size)
end
main()

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
/*
	Scala Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	//This are check that whether given array is sorted or not
	def is_sorted(arr: Array[Int], size: Int): Boolean = {
		var i: Int = 0;
		while (i < size - 1)
		{
			if (arr(i) > arr(i + 1))
			{
				//When array is unsorted
				return false;
			}
			i += 1;
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	def swap_data(arr: Array[Int], i: Int, j: Int): Unit = {
		var temp: Int = arr(i);
		arr(i) = arr(j);
		arr(j) = temp;
	}
	//shuffle array elements
	def shuffle(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
        val r = new scala.util.Random
		while (i < size)
		{
			swap_data(arr, i,r.nextInt(size-1));
			i += 1;
		}
	}
	//Perform the bogosort in given array
	def bogo_sort(arr: Array[Int], size: Int): Unit = {
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!is_sorted(arr, size))
		{
			//When array need to sort
			shuffle(arr, size);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MySort = new MySort();
		//Define an sorted array of integers
		var arr: Array[Int] = Array(11, 8, 3, 8, 32, -3, 1, 4, 0, 5);
		//Get the size of array
		var size: Int = arr.length;
		print("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.bogo_sort(arr, size);
		print("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 32 -3 1 4 0 5

 After Sort :
 -3 0 1 3 4 5 8 8 11 32
import Foundation

#if os(Linux)
    srandom(UInt32(time(nil)))
#endif
/*
	Swift Program
	Sort array elements by using Bogo Sort
*/
class MySort
{
	//Function which is display arr elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
	//This are check that whether given array is sorted or not
	func is_sorted(_ arr: [Int], _ size: Int) -> Bool
	{
		var i: Int = 0;
		while (i < size - 1)
		{
			if (arr[i] > arr[i + 1])
			{
				//When array is unsorted
				return false;
			}
			i += 1;
		}
		//When array is sorted
		return true;
	}
	//swapping the array elements in given location
	func swap_data(_ arr: inout[Int], _ i: Int, _ j: Int)
	{
		let temp: Int = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//shuffle array elements
	func shuffle(_ arr: inout[Int], _ size: Int)
	{
		var i: Int = 0;
      	var number : Int = 0;
		while (i < size)
		{
          #if os(Linux)
          	number = Int(random()%(size)) 
          #else
          	number = Int(arc4random_uniform(size)) 
          #endif
		 
          self.swap_data(&arr, i, number);
		  i += 1;
		}
	}
	//Perform the bogosort in given array
	func bogo_sort(_ arr: inout[Int], _ size: Int)
	{
		//Check given array is sorted or not
		//Loop executes until the array permutation unsorted
		while (!self.is_sorted(arr, size))
		{
			//When array need to sort
			self.shuffle(&arr, size);
		}
	}
}
func main()
{
	let obj: MySort = MySort();
	//Define an sorted array of integers
	var arr: [Int] = [11, 8, 3, 8, 32, -3, 1, 4, 0, 5];
	//Get the size of array
	let size: Int = arr.count;
	print("\n Before Sort : ");
	obj.display(arr, size);
	//Sort element
	obj.bogo_sort(&arr, size);
	print("\n After Sort : ");
	obj.display(arr, size);
}
main();

Output

 Before Sort :
  11  8  3  8  32  -3  1  4  0  5

 After Sort :
  -3  0  1  3  4  5  8  8  11  32


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved