Wave Sort in c++

 Wave Sort is a sorting algorithm that arranges elements in an array in a wave-like pattern. This algorithm is also known as the "Cocktail Sort" or "Shaker Sort." Wave Sort is considered to be an in-place sorting algorithm, which means that it does not require any additional memory space to sort the elements.

Wave Sort is a variation of the Bubble sort algorithm. The main difference between wave sort and bubble sort is that wave sort compares and swaps elements in both directions, while bubble sort compares and swaps elements in only one direction. This process of comparing and swapping elements in both directions is what gives wave sort its wave-like pattern.

The algorithm starts by comparing the first and second elements of the array. If the first element is greater than the second element, they are swapped. The process then moves to the second and third elements, and so on. Once the first pass is complete, the largest element is guaranteed to be at the end of the array. The process then repeats itself, but this time starting from the second element and ending at the second to last element. This process continues until the entire array is sorted.

The C++ implementation of the wave sort algorithm is as follows:

void waveSort(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n-i-1; j++)
        {
            if (arr[j] > arr[j+1])
            {
                swap(arr[j], arr[j+1]);
            }
            if (j > 0 && arr[j-1] > arr[j])
            {
                swap(arr[j], arr[j-1]);
            }
        }
    }
}
 

In this code, the outer for loop iterates through the array starting from the first element. The inner for loop iterates through the array starting from the second element and ending at the second to last element. The first if statement compares the current element with the next element. If the current element is greater than the next element, they are swapped. The second if statement compares the current element with the previous element. If the current element is less than the previous element, they are swapped.

The time complexity of the wave sort algorithm is O(n^2) in the worst case scenario. This is because the algorithm uses nested loops to compare and swap elements, which results in a quadratic time complexity. However, in the best case scenario, the time complexity is O(n) because the array is already sorted, and the algorithm only needs to pass through the array once to confirm that it is already sorted.

The space complexity of the wave sort algorithm is O(1), as it does not require any additional memory space to sort the elements.

In conclusion, wave sort is a simple and efficient sorting algorithm that arranges elements in a wave-like pattern. Its in-place sorting mechanism, and O(n) best case scenario make it a suitable choice for small and medium-sized datasets. However, for larger datasets, other sorting algorithms such as quicksort or merge sort might be more efficient. As always, the choice of sorting algorithm depends on the specific requirements of the problem at hand.



 

Wave Sort in c++