[go: up one dir, main page]

0% found this document useful (0 votes)
13 views3 pages

Import

This Java program defines methods to search and sort an integer array. It declares a global PRICE array with sample data, displays the array, performs a linear search to check if a user-input value exists, and applies bubble sort to sort the array. The main method calls these methods and displays messages indicating search results before and after sorting.

Uploaded by

Mlondi Dlamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Import

This Java program defines methods to search and sort an integer array. It declares a global PRICE array with sample data, displays the array, performs a linear search to check if a user-input value exists, and applies bubble sort to sort the array. The main method calls these methods and displays messages indicating search results before and after sorting.

Uploaded by

Mlondi Dlamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

public class Main {

// (a) Declare the global 1D array, PRICE, with ten elements. Populate the array with the given data
above.

static int[] PRICE = {50, 100, 50, 120, 65, 120, 90, 24, 78, 200};

public static void main(String[] args) {

// (e) Edit the main program to:

// • call display procedure

DISPLAY();

// • allow the user to input an integer value

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer value to search: ");

int searchValue = scanner.nextInt();

// • pass the value to linearSearch() as the parameter

boolean found = linearSearch(searchValue);

// • output an appropriate message to tell the user whether the search value was found or not

if (found) {

System.out.println("The value " + searchValue + " was found in the array.");

} else {

System.out.println("The value " + searchValue + " was not found in the array.");

// • call bubble sort

Bubblesort();

System.out.println("Array after bubble sort:");

DISPLAY();
}

// (b) Write a procedure called DISPLAY to output all the elements in the array.

static void DISPLAY() {

System.out.println("Array elements:");

for (int i = 0; i < PRICE.length; i++) {

System.out.print(PRICE[i] + " ");

System.out.println();

// (c) A function, linearSearch(), takes an integer as a parameter and performs a linear

// search on array PRICE to find the parameter value. It returns True if it was found and

// False if it was not found.

static boolean linearSearch(int value) {

for (int i = 0; i < PRICE.length; i++) {

if (PRICE[i] == value) {

return true;

return false;

// (d) Write a procedure Bubblesort() using the bubble sort algorithm.

static void Bubblesort() {

int n = PRICE.length;

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

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

if (PRICE[j] > PRICE[j + 1]) {

// Swap elements

int temp = PRICE[j];


PRICE[j] = PRICE[j + 1];

PRICE[j + 1] = temp;

You might also like