From f499342869a096161770e9c16b65afff66775e54 Mon Sep 17 00:00:00 2001 From: Vaibhav Gupta Date: Tue, 2 Oct 2018 16:34:16 +0530 Subject: [PATCH] Optimize bubble sort algorithm -introduced variable 'swap' for 'outer-for-loop' of bubble sort algorithm. If it remains zero after executing inner-loop, it means array is sorted, hence it does not need to run for n^2 times. - 'for(int j=0; j> n; int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + cout<<"\nEnter any "<Array[j+1]) { + swap=1; int temp=Array[j]; Array[j]=Array[j+1]; Array[j+1]=temp; } } + if(swap == 0) + { + break; + } } //Output