Ordenar una array en forma de onda

Dada una array no ordenada de enteros, ordene la array en una array similar a una onda. Una array ‘arr[0..n-1]’ se ordena en forma de onda si arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= … ..

¿Qué es una array de ondas?

C++

// A C++ program to sort an array in wave form using
// a sorting function
#include<iostream>
#include<algorithm>
using namespace std;
 
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
// This function sorts arr[0..n-1] in wave form, i.e.,
// arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5]..
void sortInWave(int arr[], int n)
{
    // Sort the input array
    sort(arr, arr+n);
 
    // Swap adjacent elements
    for (int i=0; i<n-1; i += 2)
        swap(&arr[i], &arr[i+1]);
}
 
// Driver program to test above function
int main()
{
    int arr[] = {10, 90, 49, 2, 1, 5, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    sortInWave(arr, n);
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
    return 0;
}

Java

// Java implementation of naive method for sorting
// an array in wave form.
import java.util.*;
 
class SortWave
{
    // A utility method to swap two numbers.
    void swap(int arr[], int a, int b)
    {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
 
    // This function sorts arr[0..n-1] in wave form, i.e.,
    // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]..
    void sortInWave(int arr[], int n)
    {
        // Sort the input array
        Arrays.sort(arr);
 
        // Swap adjacent elements
        for (int i=0; i<n-1; i += 2)
            swap(arr, i, i+1);
    }
 
    // Driver method
    public static void main(String args[])
    {
        SortWave ob = new SortWave();
        int arr[] = {10, 90, 49, 2, 1, 5, 23};
        int n = arr.length;
        ob.sortInWave(arr, n);
        for (int i : arr)
            System.out.print(i + " ");
    }
}
/*This code is contributed by Rajat Mishra*/

Python3

# Python function to sort the array arr[0..n-1] in wave form,
# i.e., arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5]
def sortInWave(arr, n):
     
    #sort the array
    arr.sort()
    
    # Swap adjacent elements
    for i in range(0,n-1,2):
        arr[i], arr[i+1] = arr[i+1], arr[i]
 
# Driver program
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
    print (arr[i],end=" ")
     
# This code is contributed by __Devesh Agrawal__

C#

// C# implementation of naive method
// for sorting an array in wave form.
using System;
 
class SortWave {
     
    // A utility method to swap two numbers.
    void swap(int[] arr, int a, int b)
    {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
 
    // This function sorts arr[0..n-1] in wave form, i.e.,
    // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]..
    void sortInWave(int[] arr, int n)
    {
        // Sort the input array
        Array.Sort(arr);
 
        // Swap adjacent elements
        for (int i = 0; i < n - 1; i += 2)
            swap(arr, i, i + 1);
    }
 
    // Driver method
    public static void Main()
    {
        SortWave ob = new SortWave();
        int[] arr = { 10, 90, 49, 2, 1, 5, 23 };
        int n = arr.Length;
         
        ob.sortInWave(arr, n);
        for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by vt_m.

Javascript

<script>
 
// A JavaScript program to sort an array
// in wave form using a sorting function
 
// A utility method to swap two numbers.
function swap(arr, x, y)
{
    let temp = arr[x];
    arr[x] = arr[y];
    arr[y] = temp
}
 
// This function sorts arr[0..n-1] in
// wave form, i.e.,
// arr[0] >= arr[1] <= arr[2] >=
// arr[3] <= arr[4] >= arr[5]..
function sortInWave(arr, n)
{
     
    // Sort the input array
    arr.sort((a, b) => a - b);
 
    // Swap adjacent elements
    for(let i = 0; i < n - 1; i += 2)
        swap(arr, i, i + 1);
}
 
// Driver code
let arr = [ 10, 90, 49, 2, 1, 5, 23 ];
let n = arr.length;
 
sortInWave(arr, n);
 
for(let i = 0; i < n; i++)
    document.write(arr[i] + " ");
 
// This code is contributed by Surbhi Tyagi.
 
</script>

C++14

// A O(n) program to sort an input array in wave form
#include<iostream>
using namespace std;
 
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
// This function sorts arr[0..n-1] in wave form, i.e., arr[0] >=
// arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5] ....
void sortInWave(int arr[], int n)
{
    // Traverse all even elements
    for (int i = 0; i < n; i+=2)
    {
        // If current even element is smaller than previous
        if (i>0 && arr[i-1] > arr[i] )
            swap(&arr[i], &arr[i-1]);
 
        // If current even element is smaller than next
        if (i<n-1 && arr[i] < arr[i+1] )
            swap(&arr[i], &arr[i + 1]);
    }
}
 
// Driver program to test above function
int main()
{
    int arr[] = {10, 90, 49, 2, 1, 5, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    sortInWave(arr, n);
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
    return 0;
}

Java

// A O(n) Java program to sort an input array in wave form
class SortWave
{
    // A utility method to swap two numbers.
    void swap(int arr[], int a, int b)
    {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
 
    // This function sorts arr[0..n-1] in wave form, i.e.,
    // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]....
    void sortInWave(int arr[], int n)
    {
        // Traverse all even elements
        for(int i = 0; i < n-1; i+=2){
            //swap odd and even positions
            if(i > 0 && arr[i - 1] > arr[i])
              swap(arr, i, i+1);
              if(i < n-1 && arr[i + 1] > arr[i])
              swap(arr, i, i+1);
        }
    }
 
    // Driver program to test above function
    public static void main(String args[])
    {
        SortWave ob = new SortWave();
        int arr[] = {10, 90, 49, 2, 1, 5, 23};
        int n = arr.length;
        ob.sortInWave(arr, n);
        for (int i : arr)
            System.out.print(i+" ");
    }
}
/*This code is contributed by Rajat Mishra*/

Python3

# Python function to sort the array arr[0..n-1] in wave form,
# i.e., arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5]
def sortInWave(arr, n):
     
    # Traverse all even elements
    for i in range(0, n, 2):
         
        # If current even element is smaller than previous
        if (i> 0 and arr[i] < arr[i-1]):
            arr[i],arr[i-1] = arr[i-1],arr[i]
         
        # If current even element is smaller than next
        if (i < n-1 and arr[i] < arr[i+1]):
            arr[i],arr[i+1] = arr[i+1],arr[i]
 
# Driver program
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
    print (arr[i],end=" ")
     
# This code is contributed by __Devesh Agrawal__

C#

// A O(n) C# program to sort an
// input array in wave form
using System;
 
class SortWave {
     
    // A utility method to swap two numbers.
    void swap(int[] arr, int a, int b)
    {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
 
    // This function sorts arr[0..n-1] in wave form, i.e.,
    // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]....
    void sortInWave(int[] arr, int n)
    {
        // Traverse all even elements
        for (int i = 0; i < n; i += 2) {
             
            // If current even element is smaller
            // than previous
            if (i > 0 && arr[i - 1] > arr[i])
                swap(arr, i - 1, i);
 
            // If current even element is smaller
            // than next
            if (i < n - 1 && arr[i] < arr[i + 1])
                swap(arr, i, i + 1);
        }
    }
 
    // Driver program to test above function
    public static void Main()
    {
        SortWave ob = new SortWave();
        int[] arr = { 10, 90, 49, 2, 1, 5, 23 };
        int n = arr.Length;
         
        ob.sortInWave(arr, n);
        for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by vt_m.

Javascript

<script>
 
// A O(n) program to sort
// an input array in wave form
 
// A utility method to swap two numbers.
function swap(arr, a, b)
    {
        let temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
   
    // This function sorts arr[0..n-1]
    // in wave form, i.e.,
    // arr[0] >= arr[1] <= arr[2] >=
    // arr[3] <= arr[4]....
function sortInWave( arr, n)
    {
        // Traverse all even elements
        for (let i = 0; i < n; i+=2)
        {
            // If current even element
            // is smaller than previous
            if (i>0 && arr[i-1] > arr[i] )
                swap(arr, i-1, i);
   
            // If current even element
            // is smaller than next
            if (i<n-1 && arr[i] < arr[i+1] )
                swap(arr, i, i + 1);
        }
    }
 
    // driver code
     
    let arr = [10, 90, 49, 2, 1, 5, 23];
    let n = arr.length;
    sortInWave(arr, n);
    for (let i=0; i<n; i++)
      document.write(arr[i] + " ");
     
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *