From f499342869a096161770e9c16b65afff66775e54 Mon Sep 17 00:00:00 2001 From: Vaibhav Gupta Date: Tue, 2 Oct 2018 16:34:16 +0530 Subject: [PATCH 1/3] 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 From 485d1b2bb8905639cb4436c59c3f22988197e882 Mon Sep 17 00:00:00 2001 From: Vaibhav Gupta Date: Tue, 2 Oct 2018 17:05:25 +0530 Subject: [PATCH 2/3] solve bus-error (core dumped), run-time error - Run time error on linux. - 'n' is declared but not initialized, neither its value is taken from user. - 'n' contains garbage value, thus throws run time error. --- Sorting/Insertion Sort.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sorting/Insertion Sort.cpp b/Sorting/Insertion Sort.cpp index 45e860dde..aca8d228f 100644 --- a/Sorting/Insertion Sort.cpp +++ b/Sorting/Insertion Sort.cpp @@ -6,8 +6,10 @@ using namespace std; int main() { int n; + cout<<"Enter the number of elements in your array: "; + cin>>n; int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + cout<<"\nEnter any "< Date: Sat, 9 Feb 2019 13:25:57 +0530 Subject: [PATCH 3/3] Update Bubble Sort.cpp --- Sorting/Bubble Sort.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorting/Bubble Sort.cpp b/Sorting/Bubble Sort.cpp index aa4930e82..fed01bfc5 100644 --- a/Sorting/Bubble Sort.cpp +++ b/Sorting/Bubble Sort.cpp @@ -8,13 +8,13 @@ int main() { int n; short swap_check=0; - cout << "Enter the amount of numbers to sort: "; + cout << "Enter the amount of numbers to sort: "; cin >> n; vector numbers; cout << "Enter " << n << " numbers: "; int num; - //Input + //Input for(int i=0; i> num;