Sort Array in Waveform in C
Sort Array in Waveform in C Programming Language
This page is all about converting an array into a waveform. To convert an array in waveform we can use two concepts.
- Brute Force Solution — Using Sorting
- Incrementing the loop by two
we will be learning about both of them in this page, their working and C program.
Sort Array in Waveform in C Language
A waveform sorted array is an array of integers that has been sorted in a specific way to resemble a waveform. In this sorting method, the elements in the array are arranged in a “peak-valley” pattern, where the first element is the peak, the second element is the valley, the third element is the peak again, and so on.
For example, consider the following waveform sorted array:
[10, 5, 6, 2, 20, 3, 48, 30]
In this array, the first element 10 is the peak, the second element 5 is the valley, the third element 6 is the peak again, the fourth element 2 is the valley, the fifth element 20 is the peak and so on.
The advantage of using a waveform sorted array is that it allows for efficient searching and sorting algorithms. For example, a binary search algorithm can be used to search for an element in the array with a time complexity of O(log n), which is much faster than a linear search algorithm with a time complexity of O(n). Similarly, the array can be sorted using a modified version of a merge sort algorithm that takes advantage of the waveform pattern, resulting in a time complexity of O(n log n).
In summary, a waveform sorted array is a specific way of sorting an array of integers to resemble a waveform, which allows for efficient searching and sorting algorithms.
Method 1 : Brute Force Solution (using sorting)
This is a simple method of solving this question which contains basic 2 steps and they are as follow
Step : 1 – Sort the array in ascending order.
Step : 2 – Swap all adjacent elements of the array
Let us consider the input array be {20, 30, 5, 25, 10, 15}. After sorting, we get {5, 10, 15, 20, 25, 30}. After swapping adjacent elements, we get {10, 5, 20, 15, 30, 25}.
If we plot step 1 (that is sorted array) on graph then it would look something like this.
If we plot step 2 (that is swapping adjacent elements) on graph then it would look something like this.
Method : 1 -Solution in C Programming Language
#include<stdio.h> int main () { int array[] = { 10, 49, 2, 1, 5, 23 }; int n = sizeof (array) / sizeof (array[0]); int temp; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } //step 2 for (int i = 0; i < n; i = i + 2) { temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } //printing result for (int i = 0; i < n; i++) { printf ("%d ", array[i]); } return 0; }
Output
2 1 10 5 49 23
Method 2 : Incrementing the loop by 2
The complexity of method used above is O(nLogn) which is used in the algorithms like heap sort etc. This question can be solved in O(n) complexity using single traverse of array.
Following are the steps which will be used to solve this program in O(n) complexity.
Main step : Traverse all even position elements of array (i.e. at index 0, 2, 4, 6, 8 – – -)
Condition 1 : If current element is smaller than previous odd element, swap previous and current.
Condition 2 : If current element is smaller than next odd element, swap next and current.
Method: 2 – Solution in C Programming Language
#include<stdio.h> int main () { int array[] = { 10, 49, 2, 1, 5, 23 }; int n = sizeof (array) / sizeof (array[0]); int temp; for (int i = 0; i < n; i = i + 2) { if (i > 0 && array[i - 1] > array[i]) { temp = array[i]; array[i] = array[i - 1]; array[i - 1] = temp; } if (i < n - 1 && array[i] < array[i + 1]) { temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } for (int i = 0; i < n; i++) { printf ("%d ", array[i]); } return 0; }
Output
49 2 10 1 23 5
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Dear prepinsta, please try to solve this question with odd number of elements