John Delmark Aguilar
Would you like to react to this message? Create an account in a few clicks or log in to continue.
John Delmark Aguilar

Personal Forum
 
HomeSearchLatest imagesRegisterLog in

 

 Data Sorting: BubbleSort Java

Go down 
AuthorMessage
dElmARk
Admin
Admin
dElmARk


Posts : 92
Join date : 09/04/2012

Data Sorting: BubbleSort Java Empty
PostSubject: Data Sorting: BubbleSort Java   Data Sorting: BubbleSort Java Icon_minitimeTue Oct 23, 2012 5:27 pm

BubbleSort Java

Code:

public class BubbleSort{
 public static void main(String[] args){
  int intArray[]=new int[]{5,90,35,45,150,3};
  System.out.println("Array Before Bubble Sort");
  for(int i=0; i< intArray.length; i  )
  {
  System.out.print(intArray[i]  " ");
  }
  //sort an array using bubble sort algorithm
  bubbleSort(intArray);
  System.out.print("");
  //print array after sorting using sort algorithm
  System.out.println("Array After Bubble Sort");
  for(int i=0; i< intArray.length; i  )
  {
  System.out.print(intArray[i]  " ");
  }
 }
 private static void bubbleSort(int[]intArray)
 {
  int n = intArray.length;
  int temp = 0;
  for(int i = 0; i< n; i  )
  {
  for(int j=1; j<(n-i); j  )
  {
    if(intArray[j-1] > intArray[j])
    {
      temp=intArray[j-1];
      intArray[j-1]=intArray[j];
      intArray[j]=temp;
      }
    }
  }
  }
 }

Activity:
Create a program that will interact with the user.

The program must do the following:
1. Ask the user "How many numbers will be sorted? "
2. Display the unsorted numbers.
3. Display the sorted numbers in ascending order.
4. Display the sorted numbers in descending order.
Back to top Go down
https://delmark.forumtl.com
 
Data Sorting: BubbleSort Java
Back to top 
Page 1 of 1
 Similar topics
-
» Data Sorting: SelectionSort Java
» Data Sorting: InsertionSort Java
» Java Comments
» Payroll Java
» Magic Square Java

Permissions in this forum:You cannot reply to topics in this forum
John Delmark Aguilar :: dElmARk Productions :: Java Programs :: Tutorials-
Jump to: