-
Notifications
You must be signed in to change notification settings - Fork 368
/
Gnome_sort.cpp
110 lines (79 loc) · 4.15 KB
/
Gnome_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
------------------------------------------- The Problem Statement ------------------------------------------------------
Given a unsorted array of size "n" we have to sort the array using Gnome sort.
The first line of input is the size of the array going to be given and second line represents the array elements of size "n"
INPUT :- 4
[5,3,4,2]
OUTPUT :- [2,3,4,5]
----------------------------------------------- Gnome Algorithm --------------------------------------------------------
Gnome Algorithm will be as insertion sort but not the exact same.
Firstly sorting an array in ascending order means the elements on the left side of an element should be less than the
element we choosen.
Lets take a pointer "pointer" which will be used to iterate the array whose current position is 0
1) since there is no element on left side we can move to next element by incrementing pointer by 1
to compare to say whether the array is sorted or not.
if(pointer==0) { pointer++; }
2) If the element on the left side is greater than the pointer element we need to swap.
if(array[pointer] < array[pointer-1]) { swap(array[pointer] < array[pointer-1]); pointer--; }
3) If the pointer element is equal or greater than the left side element we have to do nothing with the array just
increment the pointer by 1, so it points to the next element.
if(array[pointer] >= array[pointer-1]) { pointer++; }
Since first and third conditions are doing the same job we can combine it as a single if block to reduce the code redundancy like this,
if(pointer == 0 || array[pointer] < array[pointer-1]) { pointer++; }
We can use "Ternary Operator" to implement the above if statement too like this.
pointer = (pointer == 0 || gnome_sort[pointer] >= gnome_sort[pointer-1]) ? pointer+1 : pointer;
----------------------------------------------- Complexities ----------------------------------------------------------
Time Complexity :- BigO(n^2)
Space Complexity :- BigO(1)
Space Complexity :- BigO(n) --> If you used recursion for sorting.
*/
// Importing the neccesary things.
#include<bits/stdc++.h>
using namespace std;
// Starting of main function
int main()
{
// Declaring the size of the array
int size;
// Reading the size of the array to be sorted
cout<<"Enter Size of the Array = ";
cin>>size;
// Declaring the gnome_sort array
int gnome_sort[size];
// Reading elements from user using for loop and putting it into the gnome_sort array
for(int i=0;i<size;i++)
{
cout<<"Enter Array Element " << i+1 << " = ";
cin>>gnome_sort[i];
}
// Printing the array elements before sorting.
cout<<"\nBefore Gnome sorting the array elements are \n";
for(int i=0;i<size;i++)
{
cout<<gnome_sort[i] << " ";
}
// For iterating the gnome_sort array we need a variable that is called as pointer variable
int pointer =0;
// Gnome sorting starts
// Here I implemented gnome sort using do-while you can use while loop too.
do
{
// pointer == 0 --> It is the starting of the array so no elements on left side of the array it is the greatest we can increment.
// gnome_sort[pointer] >= gnome_sort[pointer-1] --> The elements are already in sorted position so no need to swap, we can increment.
// Here we used ternary operator to combine the two cases of pointer.
pointer = (pointer == 0 || gnome_sort[pointer] >= gnome_sort[pointer-1]) ? pointer+1 : pointer;
// gnome_sort[pointer] < gnome_sort[pointer-1] --> List element in left side is higher than the right side so we need to swap.
if (pointer < size && gnome_sort[pointer] < gnome_sort[pointer-1])
{
// Swapping the elements because the list element in left side is higher than the right side.
swap(gnome_sort[pointer],gnome_sort[pointer-1]);
pointer--;
}
} while(pointer<size); // The BASE Condition or the starting and breaking point of the program.
// Printing the array elements after gnome sorting
cout<<"\nAfter Gnome sorting the array elements are \n";
for(int i=0;i<size;i++)
{
cout<<gnome_sort[i] << " ";
}
}