0% found this document useful (0 votes)
4 views6 pages

Sort Search Java Program

Sort searching in java

Uploaded by

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

Sort Search Java Program

Sort searching in java

Uploaded by

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

Sorting and Searching: Java Programs

import java.util.Arrays;
import java.util.Scanner;

//Approach 1:
public class BubbleSorter {

// Method to perform bubble sort on an integer array


public void sortArray(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int size = sc.nextInt();

int[] data = new int[size];


System.out.println("Enter " + size + " integers:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
data[i] = sc.nextInt();
}

// Create an object of BubbleSorter class


BubbleSorter sorter = new BubbleSorter();

// Call the sortArray method using the object


sorter.sortArray(data);

System.out.println("Sorted Array (Ascending Order):");


System.out.println(Arrays.toString(data));
scanner.close();
}
}
//Approach 2:

import java.util.Scanner;
public class BubbleSort {

public static void bubbleSort(int[] arr, int n) {


// Outer loop - controls the number of passes over the array
for (int i = n - 1; i >= 0; i--) {
boolean swapped = false; // Flag to check if swapping occurred

// Inner loop - compares adjacent elements and swaps if needed


for (int j = 0; j <= i - 1; j++) {
// If the current element is greater than the next one, swap them
if (arr[j] > arr[j + 1]) {
// Swapping adjacent elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

swapped = true; // Mark that a swap occurred


}
}

// If no swaps happened in a pass, the array is already sorted


if (!swapped) {
break; // Stop sorting early to optimize performance
}
}
}

//Main method: Reads input, calls the sorting function, and prints the result.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner object for user input

// Taking array size input from the user


System.out.print("Enter the number of elements: ");
int n = sc.nextInt(); // Read the number of elements

// Creating an array to store user input


int[] arr = new int[n];

// Prompt user to enter elements


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt(); // Store each input in the array
}
// Sorting the array using Bubble Sort
bubbleSort(arr, n);

// Displaying the sorted array


System.out.println("Sorted array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println(); // Move to the next line

sc.close(); // Close Scanner object to prevent resource leak


}
}

//Approach 3:
import java.util.Arrays; // Required for Arrays.sort()
import java.util.Scanner; // Required for user input

public class ArraySorter {

private int[] dataArray; // Array to store integers

// Constructor to initialize the array size


public ArraySorter(int size) {
dataArray = new int[size];
}

// Method to input elements into the array


public void inputElements() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter " + dataArray.length + " integer elements:");
for (int i = 0; i < dataArray.length; i++) {
System.out.print("Element " + (i + 1) + ": ");
dataArray[i] = scanner.nextInt();
}
}

// Method to sort the array


public void sortArray() {
Arrays.sort(dataArray); // Uses Java's built-in sort method
System.out.println("Array sorted successfully.");
}
// Method to display the array elements
public void displayArray() {
System.out.println("Array elements: " + Arrays.toString(dataArray));
}

public static void main(String[] args) {


Scanner mainScanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = mainScanner.nextInt();

// Create an object of ArraySorter


ArraySorter sorter = new ArraySorter(size);

// Call methods using the object


sorter.inputElements();
sorter.sortArray();
sorter.displayArray();

mainScanner.close();
}
}
Linear Search

import java.util.Scanner;

public class LinearSearcher {

private int[] array;


private int size;

// Constructor to initialize the array size


public LinearSearcher(int size) {
this.size = size;
this.array = new int[size];
}

// Method to take input for the array elements


public void inputArrayElements() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter " + size + " integer elements for the array:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
// scanner.close(); // Closing the scanner here might cause issues if used
elsewhere in the same program
}

// Method to perform linear search and return the index


public int performLinearSearch(int target) {
for (int i = 0; i < size; i++) {
if (array[i] == target) {
return i; // Element found, return its index
}
}
return -1; // Element not found
}

// Method to display the search result


public void displaySearchResult(int target, int resultIndex) {
if (resultIndex != -1) {
System.out.println("Element " + target + " found at index: " + resultIndex);
} else {
System.out.println("Element " + target + " not found in the array.");
}
}
public static void main(String[] args) {
Scanner mainScanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");


int arraySize = mainScanner.nextInt();

LinearSearcher searcher = new LinearSearcher(arraySize);

searcher.inputArrayElements();

System.out.print("Enter the element to search for: ");


int targetElement = mainScanner.nextInt();

int result = searcher.performLinearSearch(targetElement);

searcher.displaySearchResult(targetElement, result);

mainScanner.close();
}
}

You might also like