Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Searching and Sorting

Linear Search Algorithm


In this article, we will discuss the Linear Search Algorithm. Searching is the process of
finding some particular element in the list. If the element is present in the list, then the
process is called successful, and the process returns the location of that element; otherwise,
the search is called unsuccessful.

Two popular search methods are Linear Search and Binary Search. So, here we will discuss
the popular searching technique, i.e., Linear Search Algorithm.

Linear search is also called as sequential search algorithm. It is the simplest searching
algorithm. In Linear search, we simply traverse the list completely and match each element of
the list with the item whose location is to be found. If the match is found, then the location of
the item is returned; otherwise, the algorithm returns NULL.

It is widely used to search an element from the unordered list, i.e., the list in which items are
not sorted. The worst-case time complexity of linear search is O(n).

The steps used in the implementation of Linear Search are listed as follows -

o First, we have to traverse the array elements using a for loop.


o In each iteration of for loop, compare the search element with the current array
element, and -
o If the element matches, then return the index of the corresponding array
element.
o If the element does not match, then move to the next element.
o If there is no match or the search element is not present in the given array, return -1.

Now, let's see the algorithm of linear search.

Algorithm

1. Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val' is the value t
o search
2. Step 1: set pos = -1
3. Step 2: set i = 1
4. Step 3: repeat step 4 while i <= n
5. Step 4: if a[i] == val
6. set pos = i
7. print pos
8. go to step 6
9. [end of if]
10. set ii = i + 1
11. [end of loop]
12. Step 5: if pos = -1
13. print "value is not present in the array "
14. [end of if]
15. Step 6: exit

Working of Linear search

Now, let's see the working of the linear search Algorithm.

To understand the working of linear search algorithm, let's take an unsorted array. It will be
easy to understand the working of linear search with an example.

Let the elements of array are -

Let the element to be searched is K = 41

Now, start from the first element and compare K with each element of the array.

The value of K, i.e., 41, is not matched with the first element of the array. So, move to the
next element. And follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of the element
matched.

Linear Search complexity


Now, let's see the time complexity of linear search in the best case, average case, and worst
case. We will also see the space complexity of linear search.

1. Time Complexity

Case Time Complexity

Best Case O(1)

Average Case O(n)

Worst Case O(n)

o Best Case Complexity - In Linear search, best case occurs when the element we are finding is at
the first position of the array. The best-case time complexity of linear search is O(1).
o Average Case Complexity - The average case time complexity of linear search is O(n).
o Worst Case Complexity - In Linear search, the worst case occurs when the element
we are looking is present at the end of the array. The worst-case in linear search could
be when the target element is not present in the given array, and we have to traverse
the entire array. The worst-case time complexity of linear search is O(n).

The time complexity of linear search is O(n) because every element in the array is compared
only once.

2. Space Complexity

Space Complexity O(1)

o The space complexity of linear search is O(1).

Implementation of Linear Search

Now, let's see the programs of linear search in different programming languages.

Program: Write a program to implement linear search in C++.

1. #include <iostream>
2. using namespace std;
3. int linearSearch(int a[], int n, int val) {
4. // Going through array linearly
5. for (int i = 0; i < n; i++)
6. {
7. if (a[i] == val)
8. return i+1;
9. }
10. return -1;
11. }
12. int main() {
13. int a[] = {69, 39, 29, 10, 56, 40, 24, 13, 51}; // given array
14. int val = 56; // value to be searched
15. int n = sizeof(a) / sizeof(a[0]); // size of array
16. int res = linearSearch(a, n, val); // Store result
17. cout<<"The elements of the array are - ";
18. for (int i = 0; i < n; i++)
19. cout<<a[i]<<" ";
20. cout<<"\nElement to be searched is - "<<val;
21. if (res == -1)
22. cout<<"\nElement is not present in the array";
23. else
24. cout<<"\nElement is present at "<<res<<" position of array";
25. return 0;
26. }

Output

Binary Search Algorithm

In this article, we will discuss the Binary Search Algorithm. Searching is the process of
finding some particular element in the list. If the element is present in the list, then the
process is called successful, and the process returns the location of that element. Otherwise,
the search is called unsuccessful.

Linear Search and Binary Search are the two popular searching techniques. Here we will
discuss the Binary Search Algorithm.

Binary search is the search technique that works efficiently on sorted lists. Hence, to search
an element into some list using the binary search technique, we must ensure that the list is
sorted.

Binary search follows the divide and conquer approach in which the list is divided into two
halves, and the item is compared with the middle element of the list. If the match is found
then, the location of the middle element is returned. Otherwise, we search into either of the
halves depending upon the result produced through the match.

NOTE: Binary search can be implemented on sorted array elements. If the list elements are
not arranged in a sorted manner, we have first to sort them.

Now, let's see the algorithm of Binary Search.

Algorithm

1. Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_bound' is th


e index of the first array element, 'upper_bound' is the index of the last array element, 'val' is t
he value to search
2. Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
3. Step 2: repeat steps 3 and 4 while beg <=end
4. Step 3: set mid = (beg + end)/2
5. Step 4: if a[mid] = val
6. set pos = mid
7. print pos
8. go to step 6
9. else if a[mid] > val
10. set end = mid - 1
11. else
12. set beg = mid + 1
13. [end of if]
14. [end of loop]
15. Step 5: if pos = -1
16. print "value is not present in the array"
17. [end of if]
18. Step 6: exit

Working of Binary search

Now, let's see the working of the Binary Search Algorithm.

To understand the working of the Binary search algorithm, let's take a sorted array. It will be
easy to understand the working of Binary search with an example.

There are two methods to implement the binary search algorithm -

o Iterative method
o Recursive method

The recursive method of binary search follows the divide and conquer approach.

Let the elements of array are -

Let the element to search is, K = 56

We have to use the below formula to calculate the mid of the array -

1. mid = (beg + end)/2

So, in the given array -

beg = 0

end = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.

Now, the element to search is found. So algorithm will return the index of the element
matched.

Binary Search complexity

Now, let's see the time complexity of Binary search in the best case, average case, and worst
case. We will also see the space complexity of Binary search.

1. Time Complexity

Case Time Complexity


Best Case O(1)

Average Case O(logn)

Worst Case O(logn)

o Best Case Complexity - In Binary search, best case occurs when the element to
search is found in first comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary search
is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when we have to
keep reducing the search space till it has only one element. The worst-case time
complexity of Binary search is O(logn).

2. Space Complexity

Space Complexity O(1)

o The space complexity of binary search is O(1).

Implementation of Binary Search

Now, let's see the programs of Binary search in different programming languages.

Program: Write a program to implement Binary search in C++.

1. #include <iostream>
2. using namespace std;
3. int binarySearch(int a[], int beg, int end, int val)
4. {
5. int mid;
6. if(end >= beg)
7. {
8. mid = (beg + end)/2;
9. /* if the item to be searched is present at middle */
10. if(a[mid] == val)
11. {
12. return mid+1;
13. }
14. /* if the item to be searched is smaller than middle, then it can only be in left subarray */
15. else if(a[mid] < val)
16. {
17. return binarySearch(a, mid+1, end, val);
18. }
19. /* if the item to be searched is greater than middle, then it can only be in right subarray */
20. else
21. {
22. return binarySearch(a, beg, mid-1, val);
23. }
24. }
25. return -1;
26. }
27. int main() {
28. int a[] = {10, 12, 24, 29, 39, 40, 51, 56, 70}; // given array
29. int val = 51; // value to be searched
30. int n = sizeof(a) / sizeof(a[0]); // size of array
31. int res = binarySearch(a, 0, n-1, val); // Store result
32. cout<<"The elements of the array are - ";
33. for (int i = 0; i < n; i++)
34. cout<<a[i]<<" ";
35. cout<<"\nElement to be searched is - "<<val;
36. if (res == -1)
37. cout<<"\nElement is not present in the array";
38. else
39. cout<<"\nElement is present at "<<res<<" position of array";
40. return 0;
41. }

42.

Output
Bubble sort Algorithm

In this article, we will discuss the Bubble sort Algorithm. The working procedure of bubble
sort is simplest. This article will be very helpful and interesting to students as they might face
bubble sort as a question in their examinations. So, it is important to discuss the topic.

Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the
intended order. It is called bubble sort because the movement of array elements is just like the
movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the
array elements in bubble sort move to the end in each iteration.

Although it is simple to use, it is primarily used as an educational tool because the


performance of bubble sort is poor in the real world. It is not suitable for large data sets. The
average and worst-case complexity of Bubble sort is O(n2), where n is a number of items.

Bubble short is majorly used where -


o complexity does not matter
o simple and shortcode is preferred

Algorithm

In the algorithm given below, suppose arr is an array of n elements. The


assumed swap function in the algorithm will swap the values of given array elements.

1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort

Working of Bubble sort Algorithm

Now, let's see the working of Bubble sort Algorithm.

To understand the working of bubble sort algorithm, let's take an unsorted array. We are
taking a short and accurate array, as we know the complexity of bubble sort is O(n2).

Let the elements of array are -


First Pass

Sorting will start from the initial two elements. Let compare them to check which is greater.

Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare 32 with 26.

Here, 26 is smaller than 36. So, swapping is required. After swapping new array will look
like -

Now, compare 32 and 35.

Here, 35 is greater than 32. So, there is no swapping required as they are already sorted.

Now, the comparison will be in between 35 and 10.

Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach at the
end of the array. After first pass, the array will be -

Now, move to the second iteration.

Second Pass
The same process will be followed for second iteration.

Here, 10 is smaller than 32. So, swapping is required. After swapping, the array will be -

Now, move to the third iteration.

Third Pass

The same process will be followed for third iteration.

Here, 10 is smaller than 26. So, swapping is required. After swapping, the array will be -

Now, move to the fourth iteration.

Fourth pass

Similarly, after the fourth iteration, the array will be -


Hence, there is no swapping required, so the array is completely sorted.

Bubble sort complexity

Now, let's see the time complexity of bubble sort in the best case, average case, and worst
case. We will also see the space complexity of bubble sort.

1. Time Complexity

Case Time Complexity

Best Case O(n)

Average Case O(n2)

Worst Case O(n2)

o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of bubble sort is O(n).
o Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. The average case time
complexity of bubble sort is O(n2).
o Worst Case Complexity - It occurs when the array elements are required to be sorted
in reverse order. That means suppose you have to sort the array elements in ascending
order, but its elements are in descending order. The worst-case time complexity of
bubble sort is O(n2).

2. Space Complexity

Space Complexity O(1)

Stable YES

o The space complexity of bubble sort is O(1). It is because, in bubble sort, an extra
variable is required for swapping.
o The space complexity of optimized bubble sort is O(2). It is because two extra
variables are required in optimized bubble sort.

Now, let's discuss the optimized bubble sort algorithm.

Optimized Bubble sort Algorithm


In the bubble sort algorithm, comparisons are made even when the array is already sorted.
Because of that, the execution time increases.

To solve it, we can use an extra variable swapped. It is set to true if swapping requires;
otherwise, it is set to false.

It will be helpful, as suppose after an iteration, if there is no swapping required, the value of
variable swapped will be false. It means that the elements are already sorted, and no further
iterations are required.

This method will reduce the execution time and also optimizes the bubble sort.

Algorithm for optimized bubble sort

1. bubbleSort(array)
2. n = length(array)
3. repeat
4. swapped = false
5. for i = 1 to n - 1
6. if array[i - 1] > array[i], then
7. swap(array[i - 1], array[i])
8. swapped = true
9. end if
10. end for
11. n = n - 1
12. until not swapped
13. end bubbleSort

Implementation of Bubble sort

Now, let's see the programs of Bubble sort in different programming languages.

Program: Write a program to implement bubble sort in C++ language.

1. #include<iostream>
2. using namespace std;
3. void print(int a[], int n) //function to print array elements
4. {
5. int i;
6. for(i = 0; i < n; i++)
7. {
8. cout<<a[i]<<" ";
9. }
10. }
11. void bubble(int a[], int n) // function to implement bubble sort
12. {
13. int i, j, temp;
14. for(i = 0; i < n; i++)
15. {
16. for(j = i+1; j < n; j++)
17. {
18. if(a[j] < a[i])
19. {
20. temp = a[i];
21. a[i] = a[j];
22. a[j] = temp;
23. }
24. }
25. }
26.
27. }
28. int main()
29. {
30. int i, j,temp;
31. int a[5] = {45, 1, 32, 13, 26};
32. int n = sizeof(a)/sizeof(a[0]);
33. cout<<"Before sorting array elements are - \n";
34. print(a, n);
35. bubble(a, n);
36. cout<<"\nAfter sorting array elements are - \n";
37. print(a, n);
38. return 0;
39. }

Output

Bucket Sort Algorithm

In this article, we will discuss the bucket sort Algorithm. The data items in the bucket sort are
distributed in the form of buckets. In coding or technical interviews for software engineers,
sorting algorithms are widely asked. So, it is important to discuss the topic.
Bucket sort is a sorting algorithm that separate the elements into multiple groups said to be
buckets. Elements in bucket sort are first uniformly divided into groups called buckets, and
then they are sorted by any other sorting algorithm. After that, elements are gathered in a
sorted manner.

The basic procedure of performing the bucket sort is given as follows -

o First, partition the range into a fixed number of buckets.


o Then, toss every element into its appropriate bucket.
o After that, sort each bucket individually by applying a sorting algorithm.
o And at last, concatenate all the sorted buckets.

The advantages of bucket sort are -

o Bucket sort reduces the no. of comparisons.


o It is asymptotically fast because of the uniform distribution of elements.

The limitations of bucket sort are -

o It may or may not be a stable sorting algorithm.


o It is not useful if we have a large array because it increases the cost.
o It is not an in-place sorting algorithm, because some extra space is required to sort the
buckets.

The best and average-case complexity of bucket sort is O(n + k), and the worst-case
complexity of bucket sort is O(n2), where n is the number of items.

Bucket sort is commonly used -

o With floating-point values.


o When input is distributed uniformly over a range.

The basic idea to perform the bucket sort is given as follows -

1. bucketSort(a[], n)
2. 1. Create 'n' empty buckets
3. 2. Do for each array element a[i]
4. 2.1. Put array elements into buckets, i.e. insert a[i] into bucket[n*a[i]]
5. 3. Sort the elements of individual buckets by using the insertion sort.
6. 4. At last, gather or concatenate the sorted buckets.
7. End bucketSort

Now, let's see the algorithm of bucket sort.


Algorithm

1. Bucket Sort(A[])
2. 1. Let B[0....n-1] be a new array
3. 2. n=length[A]
4. 3. for i=0 to n-1
5. 4. make B[i] an empty list
6. 5. for i=1 to n
7. 6. do insert A[i] into list B[n a[i]]
8. 7. for i=0 to n-1
9. 8. do sort list B[i] with insertion-sort
10. 9. Concatenate lists B[0], B[1],........, B[n-1] together in order
11. End

Scatter-gather approach

We can understand the Bucket sort algorithm via scatter-gather approach. Here, the given
elements are first scattered into buckets. After scattering, elements in each bucket are sorted
using a stable sorting algorithm. At last, the sorted elements will be gathered in order.

Let's take an unsorted array to understand the process of bucket sort. It will be easier to
understand the bucket sort via an example.

Let the elements of array are -

Now, create buckets with a range from 0 to 25. The buckets range are 0-5, 5-10, 10-15, 15-
20, 20-25. Elements are inserted in the buckets according to the bucket range. Suppose the
value of an item is 16, so it will be inserted in the bucket with the range 15-20. Similarly,
every item of the array will insert accordingly.

This phase is known to be the scattering of array elements.

Now, sort each bucket individually. The elements of each bucket can be sorted by using any
of the stable sorting algorithms.
At last, gather the sorted elements from each bucket in order

Now, the array is completely sorted.

Bucket sort complexity

Now, let's see the time complexity of bucket sort in best case, average case, and in worst
case. We will also see the space complexity of the bucket sort.

1. Time Complexity

Case Time Complexity

Best Case O(n + k)

Average Case O(n + k)

Worst Case O(n2)

o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. In Bucket sort, best case occurs when the elements are uniformly
distributed in the buckets. The complexity will be better if the elements are already
sorted in the buckets.
If we use the insertion sort to sort the bucket elements, the overall complexity will be
linear, i.e., O(n + k), where O(n) is for making the buckets, and O(k) is for sorting the
bucket elements using algorithms with linear time complexity at best case.
The best-case time complexity of bucket sort is O(n + k).
o Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. Bucket sort runs in the
linear time, even when the elements are uniformly distributed. The average case time
complexity of bucket sort is O(n + K).
o Worst Case Complexity - In bucket sort, worst case occurs when the elements are of
the close range in the array, because of that, they have to be placed in the same
bucket. So, some buckets have more number of elements than others.
The complexity will get worse when the elements are in the reverse order.
The worst-case time complexity of bucket sort is O(n2).

2. Space Complexity

Space Complexity O(n*k)

Stable YES

o The space complexity of bucket sort is O(n*k).

Implementation of bucket sort

Program: Write a program to implement bucket sort in C++.

1. #include <iostream>
2.
3. using namespace std;
4.
5. int getMax(int a[], int n) // function to get maximum element from the given array
6. {
7. int max = a[0];
8. for (int i = 1; i < n; i++)
9. if (a[i] > max)
10. max = a[i];
11. return max;
12. }
13. void bucket(int a[], int n) // function to implement bucket sort
14. {
15. int max = getMax(a, n); //max is the maximum element of array
16. int bucket[max], i;
17. for (int i = 0; i <= max; i++)
18. {
19. bucket[i] = 0;
20. }
21. for (int i = 0; i < n; i++)
22. {
23. bucket[a[i]]++;
24. }
25. for (int i = 0, j = 0; i <= max; i++)
26. {
27. while (bucket[i] > 0)
28. {
29. a[j++] = i;
30. bucket[i]--;
31. }
32. }
33. }
34. void printArr(int a[], int n) // function to print array elements
35. {
36. for (int i = 0; i < n; ++i)
37. cout<<a[i]<<" ";
38. }
39. int main()
40. {
41. int a[] = {34, 42, 74, 57, 99, 84, 9, 5};
42. int n = sizeof(a) / sizeof(a[0]); // n is the size of array
43. cout<<"Before sorting array elements are - \n";
44. printArr(a, n);
45. bucket(a, n);
46. cout<<"\nAfter sorting array elements are - \n";
47. printArr(a, n);
48. }

Output

After the execution of above code, the output will be -

Comb Sort Algorithm

In this article, we will discuss the comb sort Algorithm. Comb Sort is the advanced form of
bubble Sort. Bubble Sort compares all the adjacent values while comb sort removes all the
turtle values or small values near the end of the list.

It is a comparison-based sorting algorithm that is mainly an improvement in bubble sort. In


bubble sort, there is a comparison between the adjacent elements to sort the given array. So,
in bubble sort, the gap size between the elements that are compared is 1. Comb sort improves
the bubble sort by using a gap of size more than 1. The gap in the comb sort starts with the
larger value and then shrinks by a factor of 1.3. It means that after the completion of each
phase, the gap is divided by the shrink factor 1.3. The iteration continues until the gap is 1.
The shrink factor is found to be 1.3 by testing comb sort on 200,000 random lists. Comb sort
works better than the bubble sort, but its time complexity in average case and worst case
remains O(n2).

The process of performing the comb sort is given as follows -

STEP 1 START

STEP 2 Calculate the gap value if gap value==1 goto step 5 else goto step 3

STEP 3 Iterate over data set and compare each item with gap item then goto step 4.

STEP 4 Swap the element if require else goto step 2

STEP 5 Print the sorted array.

STEP 6 STOP

Now, let's see the algorithm of comb sort.

Algorithm

1. combSort(array, n) // n is the size of array


2. gap = n // Initialize gap size equal to the size of array
3. shrink = 1.3 // gap shrink factor
4. swapped = true
5. while (gap ! = 1 || swapped = true)
6. gap = floor(gap/1.3);
7. if gap ≤ 1
8. gap = 1
9. end if
10. swapped = false
11.
12. for every element range from 0 to n-gap, do the following -
13. if array[i] > array[i+gap]
14. swap(array[i], array[i+gap])
15. swapped = true
16. end if
17. end for loop
18. end while loop
19. end combSort

Working of comb Sort Algorithm

Now, let's see the working of the comb sort Algorithm.


To understand the working of the comb sort algorithm, let's take an unsorted array. It will be
easier to understand the comb sort via an example.

Let the elements of array are -

Now, initialize

n = 8 // size of array

gap = n

shrink = 1.3

swapped = true

First iteration

gap = floor(gap/shrink) = floor(8/1.3) = 6

swapped = false

This iteration ends here, because at i =2, the value of i + gap = 2 + 6 = 8, and there is no
element at 8th position of the array. So, after first iteration, the elements of array will be -

Now, move to iteration 2.

Second iteration

gap = floor(gap/shrink) = floor(6/1.3) = 4


swapped = false

This iteration ends here, because at i =4, the value of i + gap = 4 + 4 = 8, and there is no
element at 8th position of the array. So, after second iteration, the elements of array will be -

Now, move to iteration 3.

Third iteration

gap = floor(gap/shrink) = floor(4/1.3) = 3

swapped = false

This iteration ends here, because at i =5, the value of i + gap = 5 + 3 = 8, and there is no
element at 8th position of the array. So, after third iteration, the elements of array will be -
Now, move to iteration 4.

Fourth iteration

gap = floor(gap/shrink) = floor(3/1.3) = 2

swapped = false

This iteration ends here, because at i =6, the value of i + gap = 6 + 2 = 8, and there is no
element at 8th position of the array. So, after fourth iteration, the elements of array will be -

Now, move to iteration 5.

Fifth iteration

gap = floor(gap/shrink) = floor(2/1.3) = 1

swapped = false
After the fifth iteration, the sorted array is -

Hence, the iterations end here, and now the sorting is completed. Now, the final sorted array
is -

Comb sort complexity

Now, let's see the time complexity of Comb sort in the best case, average case, and worst
case. We will also see the space complexity of Comb sort.

1. Time Complexity

Case Time Complexity

Best Case θ(n log n)

Average Case Ω(n2/2p) where p is a number of increments.

Worst Case O(n2)

o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of comb sort is θ(n log n).
o Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. The average case time
complexity of comb sort is Ω(n2/2p), where p is a number of increments..
o Worst Case Complexity - It occurs when the array elements are required to be sorted
in reverse order. That means suppose you have to sort the array elements in ascending
order, but its elements are in descending order. The worst-case time complexity of
comb sort is O(n2).

2. Space Complexity

Space Complexity O(1)

Stable YES

o The space complexity of comb sort is O(1).

Implementation of Comb sort

Program: Write a program to implement comb sort in C++.

1. #include <iostream>
2. #include<cmath>
3. using namespace std;
4.
5. int updatedGap(int gap)
6. {
7. // Shrink gap by Shrink factor
8. gap = floor(gap/1.3);
9.
10. if (gap < 1)
11. return 1;
12. return gap;
13. }
14.
15. // Function to sort array elements using Comb Sort
16. void combSort(int a[], int n)
17. {
18. int gap = n; /* Initialize gap size equal to the size of array */
19.
20. int swapped = 1;
21.
22. while (gap != 1 || swapped == 1)
23. {
24. gap = updatedGap(gap); // find updated gap
25. // Initialize swapped as false so that we can
26. // check if swap happened or not
27. swapped = 0;
28.
29. for (int i = 0; i < n-gap; i++) /* Compare all elements with current gap */
30. {
31. if (a[i] > a[i+gap]) //swap a[i] with a[i+gap]
32. {
33. int temp = a[i];
34. a[i] = a[i+gap];
35. a[i+gap] = temp;
36. swapped = 1;
37. }
38. }
39. }
40. }
41. void printArr(int a[], int n) /* function to print array elements */
42. {
43. for (int i=0; i<n; i++)
44. cout<<a[i]<<" ";
45. }
46.
47. int main()
48. {
49. int a[] = {48, 10, 23, 43, 28, 26, 1};
50. int n = sizeof(a)/sizeof(a[0]);
51. cout<<"Before sorting array elements are - \n";
52. printArr(a, n);
53. combSort(a, n);
54. cout<<"\nAfter sorting array elements are - \n";
55. printArr(a, n);
56. return 0;
57. }

Output

You might also like