Java >> Tutoriel Java >  >> Java

Programme Java pour permuter cycliquement les éléments d'un tableau

Dans ce didacticiel, nous allons apprendre à permuter cycliquement les éléments d'un tableau, c'est-à-dire à décaler chaque élément du tableau vers la gauche d'un index. La première valeur ira dans le dernier index. Mais avant d'aller plus loin, si vous n'êtes pas familier avec les concepts du tableau, alors consultez l'article Tableaux en Java.

Saisie : 2 4 1 5 6 7

Sortie : 4 1 5 6 7 2

Programme 1 :Permuter cycliquement les éléments d'un tableau

Dans cette approche, nous permutons cycliquement les éléments d'un tableau en décalant les éléments d'une position avant.

Algorithme

  1. Commencer
  2. Déclarer un tableau.
  3. Initialiser le tableau
  4. Déclarez une variable qui stockera l'élément au premier index avant la boucle.
  5. Utilisez une boucle for pour parcourir chaque élément du tableau.
  6. Déplacez maintenant chaque élément vers la gauche.
  7. Maintenant, le dernier élément du tableau modifié sera l'élément de départ.
  8. Arrêter

Le programme ci-dessous montre comment permuter cycliquement les éléments d'un tableau en parcourant et en déplaçant les éléments. Déclarez une variable avant la boucle for pour stocker le premier élément du tableau. L'utilisation d'une boucle for décale le reste des éléments vers la gauche d'une position. Enfin, affectez la valeur stockée au dernier élément du tableau. Affichez le résultat.

/*Java program to cyclically permute the elements of an array*/
import java.util.Arrays;  
import java.util.Scanner;
import java.util.*;  

public class Main  
{  
   public static void main(String args[])   
   {  
      Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      System.out.println("Initial Array "+Arrays.toString(arr));
      int x = arr[0]; // store a[0] 
        int i; 
        for (i = 0; i < arr.length - 1; i++) { 
            
            // for other element shift left 
            arr[i] = arr[i + 1]; 
        } 
        // for the last element in the modified array 
        // it will be starting element 
        arr[i] = x; 
      
      
      System.out.println("Updated Array" +Arrays.toString(arr));  //Display the array
   }  
}  


Entrez le nombre d'éléments :
5
Entrez les éléments du tableau
6 7 8 2 3 4
Tableau initial [ 6 7 8 2 3 4 ]
Tableau mis à jour [ 7 8 2 3 4 6 ]

Programme 2 :Permuter cycliquement les éléments d'un tableau

Dans cette approche, nous permutons cycliquement les éléments d'un tableau en échangeant les éléments.

Algorithme

  1. Commencer
  2. Déclarer un tableau.
  3. Initialiser le tableau
  4. Déclarez une autre variable pour stocker le premier élément du tableau.
  5. Utilisez une boucle for pour échanger chaque élément du tableau.
  6. À chaque itération, échangez chaque élément avec le premier élément.
  7. Poursuivre cet échange d'éléments à chaque itération jusqu'à la fin de la boucle.
  8. Afficher le tableau mis à jour.
  9. Arrêtez.

Vous trouverez ci-dessous le code correspondant.

Le programme ci-dessous montre comment permuter cycliquement les éléments d'un tableau en échangeant les éléments. Tout d'abord, déclarez et initialisez un tableau. Déclarez une autre variable pour stocker le premier élément du tableau. Utilisez une boucle for pour parcourir chaque élément du tableau. Ensuite, échangez chaque élément avec le premier élément du tableau. A chaque itération, seuls deux éléments sont permutés. Continuez le processus jusqu'à la fin de la boucle pour obtenir le résultat final.

/*Java program to cyclically permute the elements of an array*/
import java.util.Arrays;  
import java.util.Scanner;
import java.util.*;  

public class Main  
{  
   public static void main(String args[])   
   {  
      Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      //Display the original array
      System.out.println("Initial Array "+Arrays.toString(arr));
      
        int first = arr[0];  //Initialize the first element of the array to a variable
        int start = 0; 
        
        // swaping each element with the first element 
        for (int i = 1; i < arr.length; i++) { 
            arr[start++] = arr[i]; 
            arr[i] = first; 
        } 
      
       //Display the updated array
      System.out.println("Updated Array" +Arrays.toString(arr));    
       
   }  
}  


Entrez le nombre d'éléments :
5
Entrez les éléments du tableau
1 4 3 6 8 2 6 7 9
Tableau initial [ 1 4 3 6 8 2 6 7 9 ]
Tableau mis à jour [ 4 3 6 8 2 6 7 9 1 ]


Balise Java