-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cocktail_Sort.cpp
68 lines (56 loc) · 1.06 KB
/
Cocktail_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
#include <stdio.h>
#include <string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n1, i,n ;
cout<<"\nEnter the number of element";
cin>>n1;
n=n1;//to store the number of elements
int a[n];
cout<<"\nEnter the element";
for(i = 0; i < n; i++)
{
cin>>a[i];
}
int j, k,temp;
for(i = 0; i < n; i++)
{
// iterate through every element from left to right
for(j = i+1; j < n; j++)
{
// Move the bigger element to the right most position as in bubble sort
if(a[j] < a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
n--;
// moving the smaller elements to left part of the array which is modification over bubble sort
for(k = n-1; k > i; k--)
{
if(a[k] < a[k-1])
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
i++;
}
cout<<"\nSorted Data ";
for (i = 0; i < n1; i++)
cout<<" "<<a[i];
return 0;
}
/* INPUT:Enter the number of element :5
Enter the element
6
5
4
3
8
OUTPUT:Sorted Data 3 5 4 6 8*/