Insertion Sort

Here given code implementation process.

//C Program 
//Insertion Sort 
#include <stdio.h>

//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 insertion_sort(int arr[],int size)
{
  for (int i = 0; i < size-1; ++i)
  {
    for (int j = i+1; j >0 ; j--)
    {
      if(arr[j-1] > arr[j])
      { //Swap array element
        swap(arr,j,j-1);
      }
      else
      {
        break;
      }  
    }
  }
 
}
//Display array elements
void display(int arr[], int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf("  %d",arr[i] );
  }
  printf("\n");
}
int main()
{
  //Define array element
  int arr[]={ 5,1,0,9,3,1,6,-3,7,1,-5,12 };

  //Get the size of array
  int size=sizeof(arr)/sizeof(arr[0]);
  printf("Before Sort : \n");
  display(arr,size);
  insertion_sort(arr,size);
  printf("After Sort : \n");
  display(arr,size);
  return 0;
}

Output

Before Sort :
  5  1  0  9  3  1  6  -3  7  1  -5  12
After Sort :
  -5  -3  0  1  1  1  3  5  6  7  9  12
/*
  C++ Program
  Insertion Sort
*/
#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 insertion_sort(int arr[], int size) {
		for (int i = 0; i < size - 1; ++i) {
			for (int j = i + 1; j > 0; j--) {
				if (arr[j - 1] > arr[j]) {
					//Swap array element
					this->swap(arr, j, j - 1);
				} else {
					break;
				}
			}
		}
	}
	//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[] = {
		5,
		1,
		0,
		9,
		3,
		1,
		6,
		-3,
		7,
		1,
		-5,
		12
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << "Before Sort : \n";
	obj.display(arr, size);
	obj.insertion_sort(arr, size);
	cout << "After Sort : \n";
	obj.display(arr, size);
	return 0;
}

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
/*
  Java Program
  Insertion Sort
*/
public class MySort {

  //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 insertion_sort(int []arr,int size)
  {
    for (int i = 0; i < size-1; ++i)
    {
      for (int j = i+1; j >0 ; j--)
      {
        if(arr[j-1] > arr[j])
        { //Swap array element
          swap(arr,j,j-1);
        }
        else
        {
          break;
        }  
      }
    }
   
  }
  //Display array elements
  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= { 5,1,0,9,3,1,6,-3,7,1,-5,12 };
    //Get the size of array
    int size = arr.length;
    System.out.print("Before Sort : \n");
    obj.display(arr,size);

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

  }
}

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
/*
  C# Program
  Insertion Sort
*/
using System;
public class MySort {
	//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 insertion_sort(int[] arr, int size) {
		for (int i = 0; i < size - 1; ++i) {
			for (int j = i + 1; j > 0; j--) {
				if (arr[j - 1] > arr[j]) {
					swap(arr, j, j - 1);
				} else {
					break;;
				}
			}
		}
	}
	//Display array elements
	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 = {
			5,
			1,
			0,
			9,
			3,
			1,
			6,
			-3,
			7,
			1,
			-5,
			12
		};
		//Get the size of array
		int size = arr.Length;
		Console.Write("Before Sort : \n");
		obj.display(arr, size);
		obj.insertion_sort(arr, size);
		Console.Write("After Sort : \n");
		obj.display(arr, size);
	}
}

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
<?php
/*
  Php Program
  Insertion Sort
*/
class MySort {
	//Swap the array element
	function swap(&$arr, $first, $second) {
		//first and second are index of array
		$temp = $arr[$first];
		$arr[$first] = $arr[$second];
		$arr[$second] = $temp;
	}

	function insertion_sort(&$arr, $size) {
		for ($i = 0; $i < $size - 1; ++$i) {
			for ($j = $i + 1; $j > 0; $j--) {
				if ($arr[$j - 1] > $arr[$j]) {
					//Swap array element
					$this->swap($arr, $j, $j - 1);
				} else {
					break;
				}
			}
		}
	}
	//Display array elements
	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(5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12);
	//Get the size of array
	$size = count($arr);
	echo("Before Sort : \n");
	$obj->display($arr, $size);
	$obj->insertion_sort($arr, $size);
	echo("After Sort : \n");
	$obj->display($arr, $size);

}
main();

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
/*
  Node Js Program
  Insertion Sort
*/
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;
	}
	insertion_sort(arr, size) {
		for (var i = 0; i < size - 1; ++i) {
			for (var j = i + 1; j > 0; j--) {
				if (arr[j - 1] > arr[j]) {
					//Swap array element
					this.swap(arr, j, j - 1);
				} else {
					break;
				}
			}
		}
	}

	//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 = [5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12];
	//Get the size of array
	var size = arr.length;
	process.stdout.write("Before Sort : \n");
	obj.display(arr, size);
	obj.insertion_sort(arr, size);
	process.stdout.write("After Sort : \n");
	obj.display(arr, size);
}

main();

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
# Python 3 Program
# Insertion Sort
class MySort :
	# Swap the array element
	def swap(self, arr, first, second) :
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	
	def insertion_sort(self, arr, size) :
		i = 0
		while (i < size - 1) :
			j = i + 1
			while (j > 0) :
				if (arr[j - 1] > arr[j]) :
					# Swap array element
					self.swap(arr, j, j - 1)
				else :
					break
				
				j -= 1
			
			i += 1
		
	
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MySort()
	arr = [5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12]
	size = len(arr)
	print("Before Sort : \n", end = "")
	obj.display(arr, size)
	obj.insertion_sort(arr, size)
	print("After Sort : \n", end = "")
	obj.display(arr, size)


if __name__ == "__main__":
	main()

Output

Before Sort :
  5  1  0  9  3  1  6  -3  7  1  -5  12
After Sort :
  -5  -3  0  1  1  1  3  5  6  7  9  12
# Ruby Program
# Insertion Sort
class MySort 
	# Swap the array element
	def swap(arr, first, second) 
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	end
	def insertion_sort(arr, size) 
		i = 0
		while (i < size - 1) 
			j = i + 1
			while (j > 0) 
				if (arr[j - 1] > arr[j]) 
					# Swap array element
					self.swap(arr, j, j - 1)
				else 
					break
				end
				j -= 1
			end
			i += 1
		end
	end
	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 = [5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12]
	size = arr.length
	print("Before Sort  :\n")
	obj.display(arr, size)
	obj.insertion_sort(arr, size)
	print("After Sort  :\n")
	obj.display(arr, size)
end
main()

Output

Before Sort  :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort  :
 -5 -3 0 1 1 1 3 5 6 7 9 12
/*
  Scala Program
  Insertion Sort
*/
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 insertion_sort(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size - 1) {
			var j: Int = i + 1;
			while (j > 0) {
				if (arr(j - 1) > arr(j)) {
					//Swap array element
					this.swap(arr, j, j - 1);
				} else {
					j = 0;
				}
				j -= 1;
			}
			i += 1;
		}
	}
	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();
		var arr: Array[Int] = Array(5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12);
		val size: Int = arr.length;
		print("Before Sort : \n");
		obj.display(arr, size);
		obj.insertion_sort(arr, size);
		print("After Sort : \n");
		obj.display(arr, size);
	}
}

Output

Before Sort :
 5 1 0 9 3 1 6 -3 7 1 -5 12
After Sort :
 -5 -3 0 1 1 1 3 5 6 7 9 12
/*
  Swift Program
  Insertion Sort
*/
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 insertion_sort(_ arr: inout [Int], _ size: Int) {
		var i: Int = 0;
		while (i < size - 1) {
			var j: Int = i + 1;
			while (j > 0) {
				if (arr[j - 1] > arr[j]) {
					//Swap array element
					self.swap(&arr, j, j - 1);
				} else {
					break;
				}
				j -= 1;
			}
			i += 1;
		}
	}
	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] = [5, 1, 0, 9, 3, 1, 6, -3, 7, 1, -5, 12];
	let size: Int = arr.count;
	print("Before Sort : \n", terminator: "");
	obj.display(arr, size);
	obj.insertion_sort(&arr, size);
	print("After Sort : \n", terminator: "");
	obj.display(arr, size);
}
main();

Output

Before Sort :
  5  1  0  9  3  1  6  -3  7  1  -5  12
After Sort :
  -5  -3  0  1  1  1  3  5  6  7  9  12

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