0% found this document useful (0 votes)
21 views

Write A Java Program Called Sort That Includes Two Function Displayarray That

This document describes a Java program that includes two functions: displayArray to display a double array, and sortArray to sort a double array. The main method creates a double array with sample values, calls displayArray to output the array before sorting, then calls sortArray to sort the array using bubble sort. Finally, it calls displayArray again to output the sorted array.

Uploaded by

neha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Write A Java Program Called Sort That Includes Two Function Displayarray That

This document describes a Java program that includes two functions: displayArray to display a double array, and sortArray to sort a double array. The main method creates a double array with sample values, calls displayArray to output the array before sorting, then calls sortArray to sort the array using bubble sort. Finally, it calls displayArray again to output the sorted array.

Uploaded by

neha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a java program called Sort that includes two function displayArray that

displays the double array and sortArray that sorts the double array. The functions
are called in main. The array is created in main and assigned the valued
obtained from command line.

Class sortArray {

public static void main(String[] args) {

//create an int array we want to sort using bubble sort algorithm


doubleintArray[] = new int[]{5,90,35,45,150,3};

//print array before sorting using bubble sort algorithm


System.out.println("Array Before Bubble Sort");
for(inti=0; i<intArray.length; i++){
System.out.print(intArray[i] + " ");
}

bubbleSort(intArray);

System.out.println("");

//print array after sorting using bubble sort algorithm


System.out.println("Array After Bubble Sort");
for(inti=0; i<intArray.length; i++){
System.out.print(intArray[i] + " ");
}

voidbubbleSort(double[] intArray) {

int n = intArray.length;
int temp = 0;

for(inti=0; i< n; i++){


for(int j=1; j < (n-i); j++){

if(intArray[j-1] >intArray[j]){
//swap the elements!
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}

}
}
}
}
Output:
Array Before Bubble Sort
5.0 90.0 35.0 45.0 150.0 3.0
Array After Bubble Sort
3.0 5.0 35.0 45.0 90.0 150.0

You might also like