Selection Sort

Here given code implementation process.

//C Program
//Selection Sort Algorithm 
#include <stdio.h>
#include <stdlib.h>

//Return maximum element in given array
int maximum_element(int arr[],int size)
{

  int max_value=arr[0],min_value=arr[0];

  for (int i = 1; i < size; ++i)
  {
    if(max_value<arr[i])
    {
      max_value=arr[i];
    }
    if(min_value>arr[i])
    {
      min_value=arr[i];
    }

  }
  if(min_value<0 && -min_value > max_value)
  {
    max_value=-min_value;
  }

  return max_value;
}
//Display array elements
void display(int arr[],int size)
{
  for (int i = 0; i < size; ++i)
  {
    //Print array value of i location
    printf("  %d",arr[i] );
  }
  printf("\n");
}
void run_sort(int arr[],int slot[],int auxiliary[], int size,int mod)
{
  //set initial slot values
  for (int i = 0; i < 10; ++i)
  {
    slot[i]=0;
  }

  for (int i = 0; i < size; ++i)
  {
    if(arr[i]<0)
    {
      printf("\nThis Algorithm are not capable to work to negative numbers\n");
      return;
    }
    //set slot element
    slot[(arr[i]/mod)%10]++;
  }
  //update slot
  for (int i = 1; i < 10; ++i)
  {

    slot[i]+=slot[i-1];

  }
  for (int i = size-1; i >=0 ; --i)
  {
    
    auxiliary[ slot[ ( arr[i] / mod) %10 ]-1 ] = arr[i];
    slot[(arr[i] / mod)%10]--;
  }
  //Assign value of actual array
  for (int i = 0; i < size; ++i)
  {
    arr[i]=auxiliary[i];
  }

}
void radix_sort(int arr[],int size)
{
  //Get the maximum element of array
  int max_value=maximum_element(arr,size);


  int mod = 1;
  //allocated slot of 0 - 9 for positive number 
  int slot[10];

  //Allocate auxiliary space to perform radix sort
  int auxiliary[size];
 
  while(max_value!=0)
  {
   
    run_sort(arr,slot,auxiliary,size,mod);

    mod = mod*10;

    //remove last digit
    max_value /= 10;

  }

}
int main()
{
 
  //Define array elements
  int arr[]= {8,2,3,8,1,3,73,121,54,23,84,13,67,23,52};
  
  //Get the size of array elements
  int size=sizeof(arr)/sizeof(arr[0]);

  printf("Before Sort : \n");
  display(arr,size);
  radix_sort(arr,size);

  printf("After Sort : \n");
  display(arr,size);

 
  return 0;
}

Output

Before Sort :
  8  2  3  8  1  3  73  121  54  23  84  13  67  23  52
After Sort :
  1  2  3  3  8  8  13  23  23  52  54  67  73  84  121
/*
  C++ Program
  Selection Sort Algorithm 
*/
#include<iostream>

using namespace std;


class MySort {
	public:

		//Swap the array element
		void swap(int arr[], int first, int second) {
			//first and second are index of array
			int temp = arr[first];
			arr[first] = arr[second];
			arr[second] = temp;
		}
	void selection_sort(int arr[], int size) {
		int min = 0;
		for (int i = 0; i < size; ++i) {
			min = i;
			for (int j = i + 1; j < size; ++j) {
				if (arr[min] > arr[j]) {
					//get the minimum element index
					min = j;
				}
			}
			if (i != min) {
				//swap minimum element index
				this->swap(arr, i, min);
			}
		}
	}
	//Display array elements
	void display(int arr[], int size) {
		for (int i = 0; i < size; ++i) {
			cout << " " << arr[i];
		}
		cout << "\n";
	}
};
int main() {
	MySort obj ;
	int arr[] = {
		8,
		2,
		3,
		8,
		1,
		3,
		73,
		121,
		54,
		23,
		84,
		13,
		67,
		23,
		52
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << "Before Sort : \n";
	obj.display(arr, size);
	obj.selection_sort(arr, size);
	cout << "After Sort : \n";
	obj.display(arr, size);
	return 0;
}

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
/*
  Java Program
  Selection Sort Algorithm 
*/
public class MySort {

  //Swap the array element
  public void swap(int []arr,int first,int second)
  {
    //first and second are index of array
    int temp = arr[first];
    arr[first] = arr[second];
    arr[second] = temp;
  }
  public void selection_sort(int []arr,int size)
  {
    int min=0;

    for (int i = 0; i < size; ++i)
    {
      min=i;

      for (int j = i+1; j <size ; ++j)
      {
        if(arr[min] >arr[j] )
        {
          //get the minimum element index
          min=j;
        }
        
      }
      if(i!=min)
      {
        //swap minimum element index
        swap(arr,i,min);
      }
    }
   
  }
  //Display array elements
  public void display(int []arr, int size)
  {
    for (int i = 0; i < size; ++i)
    {
      System.out.print("  "+arr[i] );
    }
    System.out.print("\n");
  }

  public static void main(String[] args) 
  {
  

    MySort obj = new MySort();
    //Define array elements
    int []arr= {8,2,3,8,1,3,73,121,54,23,84,13,67,23,52};
    //Get the size of array
    int size = arr.length;
    System.out.print("Before Sort : \n");
    obj.display(arr,size);

    obj.selection_sort(arr,size);
    System.out.print("After Sort : \n");
    obj.display(arr,size);

  }
}

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
/*
  C# Program
  Selection Sort Algorithm 
*/
using System;
public class MySort {
	//Swap the array element
	public void swap(int[] arr, int first, int second) {
		//first and second are index of array
		int temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	public void selection_sort(int[] arr, int size) {
		int min = 0;
		for (int i = 0; i < size; ++i) {
			min = i;
			for (int j = i + 1; j < size; ++j) {
				if (arr[min] > arr[j]) {
					//get the minimum element index
					min = j;
				}
			}
			if (i != min) {
				swap(arr, i, min);
			}
		}
	}
	//Display array elements
	public void display(int[] arr, int size) {
		for (int i = 0; i < size; ++i) {
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		MySort obj = new MySort();
		int[]
		//Define array elements
		arr = {
			8,
			2,
			3,
			8,
			1,
			3,
			73,
			121,
			54,
			23,
			84,
			13,
			67,
			23,
			52
		};
		//Get the size of array
		int size = arr.Length;
		Console.Write("Before Sort : \n");
		obj.display(arr, size);
		obj.selection_sort(arr, size);
		Console.Write("After Sort : \n");
		obj.display(arr, size);
	}
}

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
<?php
/*
  Php Program
  Selection Sort Algorithm 
*/
class MySort {
	//Swap the array element

	public 	function swap(&$arr, $first, $second) {
		//first and second are index of array
		$temp = $arr[$first];
		$arr[$first] = $arr[$second];
		$arr[$second] = $temp;
	}
	public 	function selection_sort(&$arr, $size) {
		$min = 0;
		for ($i = 0; $i < $size; ++$i) {
			$min = $i;
			for ($j = $i + 1; $j < $size; ++$j) {
				if ($arr[$min] > $arr[$j]) {
					//get the minimum element index
					$min = $j;
				}
			}
			if ($i != $min) {
				//swap minimum element index
				$this->swap($arr, $i, $min);
			}
		}
	}
	//Display array elements

	public 	function display($arr, $size) {
		for ($i = 0; $i < $size; ++$i) {
			echo(" ". $arr[$i]);
		}
		echo("\n");
	}
}

function main() {
	$obj = new MySort();
	//Define array elements
	$arr = array(8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52);
	//Get the size of array
	$size = count($arr);
	echo("Before Sort : \n");
	$obj->display($arr, $size);
	$obj->selection_sort($arr, $size);
	echo("After Sort : \n");
	$obj->display($arr, $size);

}
main();

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
/*
  Node Js Program
  Selection Sort Algorithm 
*/
class MySort {
	//Swap the array element
	swap(arr, first, second) {
		//first and second are index of array
		var temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	selection_sort(arr, size) {
		var min = 0;
		for (var i = 0; i < size; ++i) {
			min = i;
			for (var j = i + 1; j < size; ++j) {
				if (arr[min] > arr[j]) {
					//get the minimum element index
					min = j;
				}
			}

			if (i != min) {
				//swap minimum element index
				this.swap(arr, i, min);
			}
		}
	}

	//Display array elements
	display(arr, size) {
		for (var i = 0; i < size; ++i) {
			process.stdout.write(" " + arr[i]);
		}

		process.stdout.write("\n");
	}
}

function main(args) {
	var obj = new MySort();
	//Define array elements
	var arr = [8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52];
	//Get the size of array
	var size = arr.length;
	process.stdout.write("Before Sort : \n");
	obj.display(arr, size);
	obj.selection_sort(arr, size);
	process.stdout.write("After Sort : \n");
	obj.display(arr, size);
}

main();

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
#   Python 3 Program
#   Selection Sort Algorithm 
class MySort :
	# Swap the array element
	def swap(self, arr, first, second) :
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	
	def selection_sort(self, arr, size) :
		min = 0
		i = 0
		while (i < size) :
			min = i
			j = i + 1
			while (j < size) :
				if (arr[min] > arr[j]) :
					# get the minimum element index
					min = j
				
				j += 1
			
			if (i != min) :
				self.swap(arr, i, min)
			
			i += 1
		
	
	# Display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MySort()
	arr = [8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52]
	size = len(arr)
	print("Before Sort : \n", end = "")
	obj.display(arr, size)
	obj.selection_sort(arr, size)
	print("After Sort : \n", end = "")
	obj.display(arr, size)


if __name__ == "__main__":
	main()

Output

Before Sort :
  8  2  3  8  1  3  73  121  54  23  84  13  67  23  52
After Sort :
  1  2  3  3  8  8  13  23  23  52  54  67  73  84  121
# Ruby Program
# Selection Sort Algorithm 
class MySort 
	# Swap the array element
	def swap(arr, first, second) 
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	end
	def selection_sort(arr, size) 
		min = 0
		i = 0
		while (i < size) 
			min = i
			j = i + 1
			while (j < size) 
				if (arr[min] > arr[j]) 
					# get the minimum element index
					min = j
				end
				j += 1
			end
			if (i != min) 
				self.swap(arr, i, min)
			end
			i += 1
		end
	end
	# Display array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
end
def main() 
	obj = MySort.new()
	arr = [8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52]
	size = arr.length
	print("Before Sort  :\n")
	obj.display(arr, size)
	obj.selection_sort(arr, size)
	print("After Sort  :\n")
	obj.display(arr, size)
end
main()

Output

Before Sort  :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort  :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
/*
  Scala Program
  Selection Sort Algorithm 
*/
class MySort {
	//Swap the array element
	def swap(arr: Array[Int], first: Int, second: Int): Unit = {
		val temp: Int = arr(first);
		arr(first) = arr(second);
		arr(second) = temp;
	}
	def selection_sort(arr: Array[Int], size: Int): Unit = {
		var min: Int = 0;
		var i: Int = 0;
		while (i < size) {
			min = i;
			var j: Int = i + 1;
			while (j < size) {
				if (arr(min) > arr(j)) {
					//get the minimum element index
					min = j;
				}
				j += 1;
			}
			if (i != min) {
				this.swap(arr, i, min);
			}
			i += 1;
		}
	}
	//Display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MySort = new MySort();
		val arr: Array[Int] = Array(8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52);
		val size: Int = arr.length;
		print("Before Sort : \n");
		obj.display(arr, size);
		obj.selection_sort(arr, size);
		print("After Sort : \n");
		obj.display(arr, size);
	}
}

Output

Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121
/*
  Swift Program
  Selection Sort Algorithm 
*/
class MySort {
	//Swap the array element
	func swap(_ arr: inout [Int], _ first: Int, _ second: Int) {
		let temp: Int = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	func selection_sort(_ arr: inout [Int], _ size: Int) {
		var min: Int = 0;
		var i: Int = 0;
		while (i < size) {
			min = i;
			var j: Int = i + 1;
			while (j < size) {
				if (arr[min] > arr[j]) {
					//get the minimum element index
					min = j;
				}
				j += 1;
			}
			if (i != min) {
				self.swap(&arr, i, min);
			}
			i += 1;
		}
	}
	//Display array elements
	func display(_ arr: [Int], _ size: Int) {
		var i: Int = 0;
		while (i < size) {
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	let obj: MySort = MySort();
	var arr: [Int] = [8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52];
	let size: Int = arr.count;
	print("Before Sort : \n", terminator: "");
	obj.display(arr, size);
	obj.selection_sort(&arr, size);
	print("After Sort : \n", terminator: "");
	obj.display(arr, size);
}
main();

Output

Before Sort :
  8  2  3  8  1  3  73  121  54  23  84  13  67  23  52
After Sort :
  1  2  3  3  8  8  13  23  23  52  54  67  73  84  121


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