Advertisement
heysoul_sisypus

swap five num/bubble wrap algo

Mar 8th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package javaapplication89;
  8. import java.util.*;
  9. /**
  10.  *
  11.  * @author Student
  12.  */
  13. public class JavaApplication89 {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.         Scanner scan=new Scanner (System.in);
  20.        
  21.         System.out.println("Swap and Sort");
  22.         System.out.println(" ");
  23.         int num1,num2,num3,num4,num5,temp;
  24.         System.out.print("Num 1: ");
  25.         num1=scan.nextInt();
  26.         System.out.print("Num 2: ");
  27.         num2=scan.nextInt();
  28.         System.out.print("Num 3: ");
  29.         num3=scan.nextInt();
  30.         System.out.print("Num 4: ");
  31.         num4=scan.nextInt();
  32.         System.out.print("Num 5: ");
  33.         num5=scan.nextInt();
  34.        
  35.         //sort
  36.         while (num2<num1){
  37.         temp=num1;
  38.         num1=num2;
  39.         num2=temp;
  40.         }
  41.         while (num3<num1){
  42.         temp=num1;
  43.         num1=num3;
  44.         num3=temp;
  45.         }
  46.         while (num3<num2){
  47.         temp=num2;
  48.         num2=num3;
  49.         num3=temp;
  50.         }
  51.         while (num4<num1){
  52.         temp=num1;
  53.         num1=num4;
  54.         num4=temp;
  55.         }
  56.         while (num4<num2){
  57.         temp=num2;
  58.         num2=num4;
  59.         num4=temp;
  60.         }
  61.         while (num4<num3){
  62.         temp=num3;
  63.         num3=num4;
  64.         num4=temp;
  65.         }
  66.         //5,4,3,2 and 1
  67.         while (num5<num1){
  68.         temp=num1;
  69.         num1=num5;
  70.         num5=temp;
  71.         }
  72.         while (num5<num2){
  73.         temp=num2;
  74.         num2=num5;
  75.         num5=temp;
  76.         }
  77.         while (num5<num3){
  78.         temp=num3;
  79.         num3=num5;
  80.         num5=temp;
  81.         }
  82.         while (num5<num4){
  83.         temp=num4;
  84.         num4=num5;
  85.         num5=temp;
  86.         }
  87.        
  88.         //output
  89.         System.out.println("Swapped numbers:");
  90.         System.out.println(+num1);
  91.         System.out.println(+num2);
  92.         System.out.println(+num3);
  93.         System.out.println(+num4);
  94.         System.out.println(+num5);
  95.        
  96.        
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement