Difference between In-place and Out-of place algorithm
Here we will learn about the difference between the In-place algorithm and Out-of place algorithm
features | In-place algorithm | Out-of place algorithm |
1. Example | Bubble sort, comb sort, Selection sort, Insertion sort, heap sort, shell sort standard standard standard standard standard | The standard merge sort algorithm |
2. Time Complexity | o(log n) | o(n) |
3. Requirements | It requires only a few pointers. That’s why it reduces the time complexity | This algorithm requires extra space for merging. thus, it increases time complexity. |
In-place algorithm
- In the In-place algorithm, we do not require any extra memory allocation. As soon as our algorithm executes output simply overwrite the input. Thus, It saves additional memory consumption.
- In the In-place algorithm, memory requirements for the output are independent of input size and should be constant.
- In-place algorithm used in systems that runs limited memory like embedded systems. It reduces memory usage to a large extent. But also it has high time complexity.
- It counts everything in the algorithm including function calls, pointer, etc.
- It writes output in write-only memory, so it’s become more appropriate to consider only the working part of the algorithm.
Diagram for In-place algorithm
In the in-place Sorting algorithm we don’t need extra memory or indexes for sorting. This algorithm sorts the elements between them by exchanging their locations. this is how this algorithm reduces the time complexity as well as space complexity.
In step 1, It interchanges the elements between positions 1 and 5.
In step 2, interchanges the elements between positions 2 and 4 and stores it in the memory.
Out-of Place algorithm
- Out-of Place algorithm is Opposite to the In-place algorithm, it is also called an Out-in-place algorithm.
- Its output depends on input size.
- merge-sort algorithm is an example of an out-of-place algorithm. It requires extra space for merging and also increases time complexity.
Diagram for Out-of-place algorithm
In the out-of-place algorithm, it needs extra space to sort the elements. As shown in figures.
Step 1:
As we can see, for sorting element number five it created a new location for the element and placed it at the beginning of the list.
Step 2:
For element number four it allocated a new location in the memory and place it next to element number five.
Step 3:
Similarly, for element number three it allocated a new space at the memory and placed that element next to the fourth one.
Step 4:
For element number two, it also worked the same as above i.e. it created a new location and placed it next to the third one.
Step 5:
Also for element number one, it allocated a new memory location at the place near its corresponding one.
This is how the Out-of-place algorithm works and not only increases time complexity but also space complexity.
Program for Reverse the number in C++
#include<iostream.h> //these are input output stream header which access input output library files #include<conio.h> //this library file access getch method in this porgram. using namespace std; class Reverse //creating a class { int a, rev=0, rem; //creating a variables with datatype integer cout<<"Enter a number:"; //Printing the message cin>>a; //getting value from users while(a!=0) //declaring the condition i.e the number got from user is not equal to 0 { rem=a%10; //if condition will be true this loop execute rev=rev*10+rem; a/=10; } cout<<"Reversed Number is:"<<rev<<endl; //printing the reverse number with message getch(); //using for diaplying the output return 0; //returing the value }
Output
Enter a number:856 Reversed number:658
Program for reverse number in python
num=7856 //getting number for reverse rev=0 while num!=0: //applying condtion i.e number is not equal to zero rem=num%10 //if condition will true this while loop will execute rev=rev*10+rem num//=10 print("Reversed number:"+str(rev)) //printing the number after reverse
Output
Reversed number:6857
Program for reverse number in java
Enlightenerpublic class RevNum //creating a public class { public static void main(String args[]) { int num=7586, rev=0;//creating a paramter in integer data type while(num!=0) //applying condition i.e. if number is not equal to 0 { int rem=num%10; // rem is using as a varibale for remainder in integer data type rev=rev*10+rem; //rev is using for reverse num/=10; } System.out.println("Reversed number:"+rev); //here we are printing the number after reverse } }
Output
Reversed number:6857