Reorder an array according to given indexes

Here given code implementation process.

// C Program 
// Reorder an array according to given indexes
// Recursive solution
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf("  %d", arr[i]);
    }
    printf("\n");
}

// Recursive solution to reorder array by given indexes
void reorder(int arr[],int indexes[], int size, int i)
{
    if(i < size)
    {
        // Get the data which is assigning of another location
        int data = arr[i];

        // Visit to next upcoming element
        reorder(arr,indexes,size,i+1);

        // put data
        arr[indexes[i]] = data;

    }
}

int main(int argc, char const *argv[])
{

    // Define array of integer elements
    int arr[]      = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    // reorder location
    int indexes[] = { 5, 2, 0, 7, 9, 1, 6, 3, 8, 4};
 
   
    // Get the size
    int size = sizeof(arr)/sizeof(arr[0]);

    printf("  Array Element \n");
    printArray(arr,size);

    printf("\n  Indexes \n");
    printArray(indexes,size);

    reorder(arr,indexes,size,0);
    printf("\n  After Reorder \n");
    printArray(arr,size);

    return 0;
}

Output

  Array Element
  1  2  3  4  5  6  7  8  9  10

  Indexes
  5  2  0  7  9  1  6  3  8  4

  After Reorder
  3  6  2  8  10  1  7  4  9  5
/*
    Java Program
    Reorder an array according to given indexes
    Recursive solution
*/
public class Arrangement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Recursive solution to reorder array by given indexes
	public void reorder(int[] arr, int[] indexes, int size, int i)
	{
		if (i < size)
		{
			// Get the data which is assigning of another location
			int data = arr[i];
			// Visit to next upcoming element
			reorder(arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
	public static void main(String args[])
	{
		Arrangement task = new Arrangement();
		// Define array of integer elements
		int[] arr = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
		};
		// reorder location
		int[] indexes = {
			5 , 2 , 0 , 7 , 9 , 1 , 6 , 3 , 8 , 4
		};
		// Get the size
		int size = arr.length;
		System.out.print(" Given Element \n");
		task.printArray(arr, size);
		System.out.print("\n Indexes \n");
		task.printArray(indexes, size);
		task.reorder(arr, indexes, size, 0);
		System.out.print("\n After Reorder \n");
		task.printArray(arr, size);
	}
}

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Recursive solution to reorder array by given indexes
	void reorder(int arr[], int indexes[], int size, int i)
	{
		if (i < size)
		{
			// Get the data which is assigning of another location
			int data = arr[i];
			// Visit to next upcoming element
			this->reorder(arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
};
int main()
{
	Arrangement task = Arrangement();
	// Define array of integer elements
	int arr[] = {
		1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
	};
	// reorder location
	int indexes[] = {
		5 , 2 , 0 , 7 , 9 , 1 , 6 , 3 , 8 , 4
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << " Given Element \n";
	task.printArray(arr, size);
	cout << "\n Indexes \n";
	task.printArray(indexes, size);
	task.reorder(arr, indexes, size, 0);
	cout << "\n After Reorder \n";
	task.printArray(arr, size);
	return 0;
}

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
// Include namespace system
using System;
/*
    C# Program
    Reorder an array according to given indexes
    Recursive solution
*/
public class Arrangement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Recursive solution to reorder array by given indexes
	public void reorder(int[] arr, int[] indexes, int size, int i)
	{
		if (i < size)
		{
			// Get the data which is assigning of another location
			int data = arr[i];
			// Visit to next upcoming element
			reorder(arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
	public static void Main(String[] args)
	{
		Arrangement task = new Arrangement();
		// Define array of integer elements
		int[] arr = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
		};
		// reorder location
		int[] indexes = {
			5 , 2 , 0 , 7 , 9 , 1 , 6 , 3 , 8 , 4
		};
		// Get the size
		int size = arr.Length;
		Console.Write(" Given Element \n");
		task.printArray(arr, size);
		Console.Write("\n Indexes \n");
		task.printArray(indexes, size);
		task.reorder(arr, indexes, size, 0);
		Console.Write("\n After Reorder \n");
		task.printArray(arr, size);
	}
}

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
<?php
/*
    Php Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Recursive solution to reorder array by given indexes
	public	function reorder( & $arr, & $indexes, $size, $i)
	{
		if ($i < $size)
		{
			// Get the data which is assigning of another location
			$data = $arr[$i];
			// Visit to next upcoming element
			$this->reorder($arr, $indexes, $size, $i + 1);
			// put data
			$arr[$indexes[$i]] = $data;
		}
	}
}

function main()
{
	$task = new Arrangement();
	// Define array of integer elements
	$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	// reorder location
	$indexes = array(5, 2, 0, 7, 9, 1, 6, 3, 8, 4);
	// Get the size
	$size = count($arr);
	echo " Given Element \n";
	$task->printArray($arr, $size);
	echo "\n Indexes \n";
	$task->printArray($indexes, $size);
	$task->reorder($arr, $indexes, $size, 0);
	echo "\n After Reorder \n";
	$task->printArray($arr, $size);
}
main();

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
/*
    Node Js Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Recursive solution to reorder array by given indexes
	reorder(arr, indexes, size, i)
	{
		if (i < size)
		{
			// Get the data which is assigning of another location
			var data = arr[i];
			// Visit to next upcoming element
			this.reorder(arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
}

function main()
{
	var task = new Arrangement();
	// Define array of integer elements
	var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	// reorder location
	var indexes = [5, 2, 0, 7, 9, 1, 6, 3, 8, 4];
	// Get the size
	var size = arr.length;
	process.stdout.write(" Given Element \n");
	task.printArray(arr, size);
	process.stdout.write("\n Indexes \n");
	task.printArray(indexes, size);
	task.reorder(arr, indexes, size, 0);
	process.stdout.write("\n After Reorder \n");
	task.printArray(arr, size);
}
main();

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
#  Python 3 Program
#  Reorder an array according to given indexes
#  Recursive solution

class Arrangement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Recursive solution to reorder array by given indexes
	def reorder(self, arr, indexes, size, i) :
		if (i < size) :
			#  Get the data which is assigning of another location
			data = arr[i]
			#  Visit to next upcoming element
			self.reorder(arr, indexes, size, i + 1)
			#  put data
			arr[indexes[i]] = data
		
	

def main() :
	task = Arrangement()
	#  Define array of integer elements
	arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	#  reorder location
	indexes = [5, 2, 0, 7, 9, 1, 6, 3, 8, 4]
	#  Get the size
	size = len(arr)
	print(" Given Element ")
	task.printArray(arr, size)
	print("\n Indexes ")
	task.printArray(indexes, size)
	task.reorder(arr, indexes, size, 0)
	print("\n After Reorder ")
	task.printArray(arr, size)

if __name__ == "__main__": main()

Output

 Given Element
   1   2   3   4   5   6   7   8   9   10

 Indexes
   5   2   0   7   9   1   6   3   8   4

 After Reorder
   3   6   2   8   10   1   7   4   9   5
#  Ruby Program
#  Reorder an array according to given indexes
#  Recursive solution

class Arrangement 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Recursive solution to reorder array by given indexes
	def reorder(arr, indexes, size, i) 
		if (i < size) 
			#  Get the data which is assigning of another location
			data = arr[i]
			#  Visit to next upcoming element
			self.reorder(arr, indexes, size, i + 1)
			#  put data
			arr[indexes[i]] = data
		end

	end

end

def main() 
	task = Arrangement.new()
	#  Define array of integer elements
	arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	#  reorder location
	indexes = [5, 2, 0, 7, 9, 1, 6, 3, 8, 4]
	#  Get the size
	size = arr.length
	print(" Given Element \n")
	task.printArray(arr, size)
	print("\n Indexes \n")
	task.printArray(indexes, size)
	task.reorder(arr, indexes, size, 0)
	print("\n After Reorder \n")
	task.printArray(arr, size)
end

main()

Output

 Given Element 
  1  2  3  4  5  6  7  8  9  10

 Indexes 
  5  2  0  7  9  1  6  3  8  4

 After Reorder 
  3  6  2  8  10  1  7  4  9  5
/*
    Scala Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Recursive solution to reorder array by given indexes
	def reorder(arr: Array[Int], indexes: Array[Int], size: Int, i: Int): Unit = {
		if (i < size)
		{
			// Get the data which is assigning of another location
			var data: Int = arr(i);
			// Visit to next upcoming element
			this.reorder(arr, indexes, size, i + 1);
			// put data
			arr(indexes(i)) = data;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Arrangement = new Arrangement();
		// Define array of integer elements
		var arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		// reorder location
		var indexes: Array[Int] = Array(5, 2, 0, 7, 9, 1, 6, 3, 8, 4);
		// Get the size
		var size: Int = arr.length;
		print(" Given Element \n");
		task.printArray(arr, size);
		print("\n Indexes \n");
		task.printArray(indexes, size);
		task.reorder(arr, indexes, size, 0);
		print("\n After Reorder \n");
		task.printArray(arr, size);
	}
}

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5
/*
    Swift 4 Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Recursive solution to reorder array by given indexes
	func reorder(_ arr: inout[Int], _ indexes: [Int], _ size: Int, _ i: Int)
	{
		if (i < size)
		{
			// Get the data which is assigning of another location
			let data: Int = arr[i];
			// Visit to next upcoming element
			self.reorder(&arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
}
func main()
{
	let task: Arrangement = Arrangement();
	// Define array of integer elements
	var arr: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	// reorder location
	let indexes: [Int] = [5, 2, 0, 7, 9, 1, 6, 3, 8, 4];
	// Get the size
	let size: Int = arr.count;
	print(" Given Element ");
	task.printArray(arr, size);
	print("\n Indexes ");
	task.printArray(indexes, size);
	task.reorder(&arr, indexes, size, 0);
	print("\n After Reorder ");
	task.printArray(arr, size);
}
main();

Output

 Given Element
   1   2   3   4   5   6   7   8   9   10

 Indexes
   5   2   0   7   9   1   6   3   8   4

 After Reorder
   3   6   2   8   10   1   7   4   9   5
/*
    Kotlin Program
    Reorder an array according to given indexes
    Recursive solution
*/
class Arrangement
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Recursive solution to reorder array by given indexes
	fun reorder(arr: Array<Int>, indexes: Array<Int>, size: Int, i: Int): Unit
	{
		if (i<size)
		{
			// Get the data which is assigning of another location
			var data: Int = arr[i];
			// Visit to next upcoming element
			this.reorder(arr, indexes, size, i + 1);
			// put data
			arr[indexes[i]] = data;
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: Arrangement = Arrangement();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	// reorder location
	var indexes: Array<Int> = arrayOf(5, 2, 0, 7, 9, 1, 6, 3, 8, 4);
	// Get the size
	var size: Int = arr.count();
	print(" Given Element \n");
	task.printArray(arr, size);
	print("\n Indexes \n");
	task.printArray(indexes, size);
	task.reorder(arr, indexes, size, 0);
	print("\n After Reorder \n");
	task.printArray(arr, size);
}

Output

 Given Element
  1  2  3  4  5  6  7  8  9  10

 Indexes
  5  2  0  7  9  1  6  3  8  4

 After Reorder
  3  6  2  8  10  1  7  4  9  5


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