Question

What are the basic sorting algorithms in java ? (What are considered basic sorting algorithm in...

What are the basic sorting algorithms in java ? (What are considered basic sorting algorithm in java?)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
The basic sorting algorithms in java is bubble sort.

================================

import java.util.Scanner;

public class BubbleSort {

  //Bubble sort function
  public static void bubbleSort(int[] array) {
    for (int i = 0; i < array.length - 1; i++)
      for (int j = 0;j<array.length-i-1;j++)
        if (array[j] > (array[j + 1])) {
          int temp = array[j];
          array[j] = array[j + 1];
          array[j + 1] = temp;
        }
  }

  public static int[] readInput(){
    Scanner scanner = new Scanner(System.in);

    //Reading size of array
    int n;
    System.out.println("Enter number of values: ");
    n = scanner.nextInt();

    //Reading n number of values to array
    System.out.println("Enter "+n+" values");
    int arr[] = new int[n];
    for(int i = 0;i<n;i++){
      arr[i] = scanner.nextInt();
    }
    return arr;
  }

  public static void main(String args[]) {
    int arr[] = readInput();
    //Calling bubble sort
    bubbleSort(arr);

    //Printing the sorted array after calling bubbleSort function
    System.out.println("Sorted array:");
    for(int i = 0;i<arr.length;i++)
      System.out.print(arr[i]+" ");
    System.out.println();
  }
}

Add a comment
Know the answer?
Add Answer to:
What are the basic sorting algorithms in java ? (What are considered basic sorting algorithm in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT