0% found this document useful (0 votes)
32 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 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
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy