From 2342866244accadb8706f05473ff98b0d30681a5 Mon Sep 17 00:00:00 2001 From: Parjanya Aditya Shukla <46294122+parjanyaacoder@users.noreply.github.com> Date: Fri, 2 Oct 2020 16:52:40 +0530 Subject: [PATCH] Create PancakeSort.java --- Sorting/PancakeSort.java | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Sorting/PancakeSort.java diff --git a/Sorting/PancakeSort.java b/Sorting/PancakeSort.java new file mode 100644 index 00000000..65c11f65 --- /dev/null +++ b/Sorting/PancakeSort.java @@ -0,0 +1,50 @@ +import java.util.*; +class Main +{ + static void reverseArray(int a[],int n) //This functions reverses the array upto index n + { + int temp[]=new int[n+1]; // temp is the temporary array + for(int i=0;i<=n;i++) + { + temp[i]=a[n-i]; + } + for(int i=0;i<=n;i++) + a[i]=temp[i]; + } + static int findmax(int a[],int n) //Function to find index of max element upto index n + { + int max=a[0];int j=0; //j stores the index of max element + for(int i=1;i<=n;i++) + if(a[i]>max) + {max=a[i]; + j=i; + } + return j; + + } + static void pancakesort(int a[],int n) + { int currentLength; + for(currentLength=n;currentLength>0;--currentLength) + { + int j= findmax(a,currentLength-1); + if(j!=currentLength) + { + reverseArray(a,j); + reverseArray(a,currentLength-1); + } + } + } + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + int i,j,k,l,m,n; + n=sc.nextInt(); + int array[]=new int[n]; + for(i=0;i