Count of elements which are not at the correct position

Here given code implementation process.

// Java Program 
// Count of elements which are not at the correct position
import java.util.Arrays;
public class Unsorted
{
	public void printData(int[] arr, int n)
	{
		for (int i = 0; i < n; i++)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Count all element which is not exist in sorted place
	public void position(int[] arr, int n)
	{
		if (n < 1)
		{
			// Invalid inputs
			return;
		}
		// Create an temp auxiliary array
		int[] temp = new int[n];
		int count = 0;
		int i = 0;
		// iterate the loop through by size
		for (i = 0; i < n; i++)
		{
			temp[i] = arr[i];
		}
		// Sort temp array
		Arrays.sort(temp);
		// iterate the loop through by size
		// compare and count the all unsorted elements
		for (i = 0; i < n; i++)
		{
			if (temp[i] != arr[i])
			{
				// Count different elements
				count++;
			}
		}
		// Display array elements
		printData(arr, n);
		// Display calculated result
		System.out.print("  Result : "+ count);
	}
	public static void main(String[] args)
	{
		Unsorted task = new Unsorted();
		// Define array of integer elements
		int[] arr = {
			1 , 7 , 10 , 8 , 9 , 11 , 18 , 17 , 19
		};
		// Get the number of element
		int n = arr.length;
		// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
		// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
		//  5 element which is not in correct position	
		task.position(arr, n);
	}
}

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
// Include header file
#include <iostream>
#include <algorithm>
using namespace std;
// C++ Program
// Count of elements which are not at the correct position
class Unsorted
{
	public: void printData(int arr[], int n)
	{
		for (int i = 0; i < n; i++)
		{
			cout << "  " << arr[i];
		}
		cout << "\n";
	}
	// Count all element which is not exist in sorted place
	void position(int arr[], int n)
	{
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary space
		int *temp = new int[n];
		int count = 0;
		int i = 0;
		// iterate the loop through by size
		for (i = 0; i < n; i++)
		{
			temp[i] = arr[i];
		}
		// Sort temp array
      	sort(arr, arr+n);
		// iterate the loop through by size
		// compare and count the all unsorted elements
		for (i = 0; i < n; i++)
		{
			if (temp[i] != arr[i])
			{
				// Count different elements
				count++;
			}
		}
		// Display array elements
		this->printData(arr, n);
		// Display calculated result
		cout << "  Result : " << count;
	}
};
int main()
{
	Unsorted task = Unsorted();
	// Define array of integer elements
	int arr[] = {
		1 , 7 , 10 , 8 , 9 , 11 , 18 , 17 , 19
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
	// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
	//  5 element which is not in correct position
	task.position(arr, n);
	return 0;
}

Output

  1  7  8  9  10  11  17  18  19
  Result : 5
// Include namespace system
using System;
// C# Program
// Count of elements which are not at the correct position
public class Unsorted
{
	public void printData(int[] arr, int n)
	{
		for (int i = 0; i < n; i++)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Count all element which is not exist in sorted place
	public void position(int[] arr, int n)
	{
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		int[] temp = new int[n];
		int count = 0;
		int i = 0;
		// iterate the loop through by size
		for (i = 0; i < n; i++)
		{
			temp[i] = arr[i];
		}
		// Sort temp array
		Array.Sort(temp);
		// iterate the loop through by size
		// compare and count the all unsorted elements
		for (i = 0; i < n; i++)
		{
			if (temp[i] != arr[i])
			{
				// Count different elements
				count++;
			}
		}
		// Display array elements
		printData(arr, n);
		// Display calculated result
		Console.Write("  Result : " + count);
	}
	public static void Main(String[] args)
	{
		Unsorted task = new Unsorted();
		// Define array of integer elements
		int[] arr = {
			1 , 7 , 10 , 8 , 9 , 11 , 18 , 17 , 19
		};
		// Get the number of element
		int n = arr.Length;
		// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
		// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
		//  5 element which is not in correct position
		task.position(arr, n);
	}
}

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
<?php
// Php Program
// Count of elements which are not at the correct position
class Unsorted
{
	public	function printData( & $arr, $n)
	{
		for ($i = 0; $i < $n; $i++)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Count all element which is not exist in sorted place
	public	function position( & $arr, $n)
	{
		// Invalid inputs
		if ($n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		$temp = array_fill(0, $n, 0);
		$count = 0;
		$i = 0;
		// iterate the loop through by size
		for ($i = 0; $i < $n; $i++)
		{
			$temp[$i] = $arr[$i];
		}
		sort($temp);
		// iterate the loop through by size
		// compare and count the all unsorted elements
		for ($i = 0; $i < $n; $i++)
		{
			if ($temp[$i] != $arr[$i])
			{
				// Count different elements
				$count++;
			}
		}
		// Display array elements
		$this->printData($arr, $n);
		// Display calculated result
		echo "  Result : ". $count;
	}
}

function main()
{
	$task = new Unsorted();
	// Define array of integer elements
	$arr = array(1, 7, 10, 8, 9, 11, 18, 17, 19);
	// Get the number of element
	$n = count($arr);
	$task->position($arr, $n);
}
main();

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
// Node Js Program
// Count of elements which are not at the correct position
class Unsorted
{
	printData(arr, n)
	{
		for (var i = 0; i < n; i++)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Count all element which is not exist in sorted place
	position(arr, n)
	{
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		var temp = Array(n).fill(0);
		var count = 0;
		var i = 0;
		// iterate the loop through by size
		for (i = 0; i < n; i++)
		{
			temp[i] = arr[i];
		}
      
      	const compare = (a, b) => a - b;
		// Sort temp array
		temp = temp.sort(compare);
      
		// iterate the loop through by size
		// compare and count the all unsorted elements
		for (i = 0; i < n; i++)
		{
			if (temp[i] != arr[i])
			{
				// Count different elements
				count++;
			}
		}
		// Display array elements
		this.printData(arr, n);
		// Display calculated result
		process.stdout.write("  Result : " + count);
	}
}

function main()
{
	var task = new Unsorted();
	// Define array of integer elements
	var arr = [1, 7, 10, 8, 9, 11, 18, 17, 19];
	// Get the number of element
	var n = arr.length;
	// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
	// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
	//  5 element which is not in correct position
	task.position(arr, n);
}
main();

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
#  Python 3 Program
#  Count of elements which are not at the correct position
class Unsorted :
	def printData(self, arr, n) :
		i = 0
		while (i < n) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Count all element which is not exist in sorted place
	def position(self, arr, n) :
		#  Invalid inputs
		if (n < 1) :
			return
		
		#  Create an temp auxiliary list
		temp = [0] * (n)
		count = 0
		i = 0
		#  iterate the loop through by size
		i = 0
		while (i < n) :
			temp[i] = arr[i]
			i += 1
		
		#  Sort temp list
		temp.sort()
		
		#  iterate the loop through by size
		#  compare and count the all unsorted elements
		i = 0
		while (i < n) :
			if (temp[i] != arr[i]) :
				#  Count different elements
				count += 1
			
			i += 1
		
		#  Display list elements
		self.printData(arr, n)
		#  Display calculated result
		print("  Result : ", count, end = "")
	

def main() :
	task = Unsorted()
	#  Define list of integer elements
	arr = [1, 7, 10, 8, 9, 11, 18, 17, 19]
	#  Get the number of element
	n = len(arr)
	#  : 1,7, 10, 8 , 9, 11, 18, 17, 19 actual list
	#  : 1,7, 8 , 9, 10, 11, 17, 18, 19 after sort
	#   5 element which is not in correct position
	task.position(arr, n)

if __name__ == "__main__": main()

Output

   1   7   10   8   9   11   18   17   19
  Result :  5
#  Ruby Program
#  Count of elements which are not at the correct position
class Unsorted 
	def printData(arr, n) 
		i = 0
		while (i < n) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Count all element which is not exist in sorted place
	def position(arr, n) 
		#  Invalid inputs
		if (n < 1) 
			return
		end

		#  Create an temp auxiliary array
		temp = Array.new(n) {0}
		count = 0
		i = 0
		#  iterate the loop through by size
		i = 0
		while (i < n) 
			temp[i] = arr[i]
			i += 1
		end

		#  Sort temp array
		temp = temp.sort
		#  iterate the loop through by size
		#  compare and count the all unsorted elements
		i = 0
		while (i < n) 
			if (temp[i] != arr[i]) 
				#  Count different elements
				count += 1
			end

			i += 1
		end

		#  Display array elements
		self.printData(arr, n)
		#  Display calculated result
		print("  Result : ", count)
	end

end

def main() 
	task = Unsorted.new()
	#  Define array of integer elements
	arr = [1, 7, 10, 8, 9, 11, 18, 17, 19]
	#  Get the number of element
	n = arr.length
	#   1,7, 10, 8 , 9, 11, 18, 17, 19  actual array
	#   1,7, 8 , 9, 10, 11, 17, 18, 19  after sort
	#   5 element which is not in correct position
	task.position(arr, n)
end

main()

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
// Scala Program
// Count of elements which are not at the correct position
class Unsorted
{
	def printData(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Count all element which is not exist in sorted place
	def position(arr: Array[Int], n: Int): Unit = {
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		var temp: Array[Int] = Array.fill[Int](n)(0);
		var count: Int = 0;
		var i: Int = 0;
		// iterate the loop through by size
		i = 0;
		while (i < n)
		{
			temp(i) = arr(i);
			i += 1;
		}
		// Sort temp array
		temp = temp.sorted;
		// iterate the loop through by size
		// compare and count the all unsorted elements
		i = 0;
		while (i < n)
		{
			if (temp(i) != arr(i))
			{
				// Count different elements
				count += 1;
			}
			i += 1;
		}
		// Display array elements
		this.printData(arr, n);
		// Display calculated result
		print("  Result : " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Unsorted = new Unsorted();
		// Define array of integer elements
		var arr: Array[Int] = Array( 1, 7, 10, 8, 9, 11, 18, 17, 19);
		// Get the number of element
		var n: Int = arr.length;
		// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
		// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
		//  5 element which is not in correct position
		task.position(arr, n);
	}
}

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
// Swift 4 Program
// Count of elements which are not at the correct position
class Unsorted
{
	func printData(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Count all element which is not exist in sorted place
	func position(_ arr: [Int], _ n: Int)
	{
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		var temp: [Int] = Array(repeating: 0, count: n);
		var count: Int = 0;
		var i: Int = 0;
		// iterate the loop through by size
		i = 0;
		while (i < n)
		{
			temp[i] = arr[i];
			i += 1;
		}
		// Sort temp array
		temp.sort();
		// iterate the loop through by size
		// compare and count the all unsorted elements
		i = 0;
		while (i < n)
		{
			if (temp[i]  != arr[i])
			{
				// Count different elements
				count += 1;
			}
			i += 1;
		}
		// Display array elements
		self.printData(arr, n);
		// Display calculated result
		print("  Result : ", count, terminator: "");
	}
}
func main()
{
	let task: Unsorted = Unsorted();
	// Define array of integer elements
	let arr: [Int] = [1, 7, 10, 8, 9, 11, 18, 17, 19];
	// Get the number of element
	let n: Int = arr.count;
	// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
	// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
	//  5 element which is not in correct position
	task.position(arr, n);
}
main();

Output

   1   7   10   8   9   11   18   17   19
  Result :  5
// Kotlin Program
// Count of elements which are not at the correct position
class Unsorted
{
	fun printData(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Count all element which is not exist in sorted place
	fun position(arr: Array < Int > , n: Int): Unit
	{
		// Invalid inputs
		if (n < 1)
		{
			return;
		}
		// Create an temp auxiliary array
		var temp: Array < Int > = Array(n)
		{
			0
		};
		var count: Int = 0;
		var i: Int = 0;
		// iterate the loop through by size
		while (i < n)
		{
			temp[i] = arr[i];
			i += 1;
		}
		// Sort temp array
		temp.sort();
		// iterate the loop through by size
		// compare and count the all unsorted elements
		i = 0;
		while (i < n)
		{
			if (temp[i] != arr[i])
			{
				// Count different elements
				count += 1;
			}
			i += 1;
		}
		// Display array elements
		this.printData(arr, n);
		// Display calculated result
		print("  Result : " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Unsorted = Unsorted();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(1, 7, 10, 8, 9, 11, 18, 17, 19);
	// Get the number of element
	var n: Int = arr.count();
	// { 1,7, 10, 8 , 9, 11, 18, 17, 19} actual array
	// { 1,7, 8 , 9, 10, 11, 17, 18, 19} after sort
	//  5 element which is not in correct position
	task.position(arr, n);
}

Output

  1  7  10  8  9  11  18  17  19
  Result : 5
Compare the sorted and unsorted element


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