Skip to main content

Posts

Showing posts with the label bubble sort example

Sorting in Java | Bubble Sort Algorithm | Java Placement Course 16

Sorting in JAVA Bubble Sort Bubble Sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It gets its name from the way the smaller elements "bubble" to the top of the list. Here is the basic algorithm: 1) Start with an unsorted list of n elements 2) Loop through the list n-1 times (the outer loop). 3) For each iteration of the outer loop, loop through the unsorted portion of the list (the inner loop). 4) Compare each adjacent pair of elements. If they are in the wrong order, swap them. 5) After each iteration of the inner loop, the largest element in the unsorted portion of the list will "bubble up" to the end of the list. 6) After the outer loop has finished, the list will be sorted. Idea: if arr[i] > arr[i+1] swap them. To place the element in their respective position, we have to do the following operation N-1 times.  Time Complexity: O(N 2 ) Code ...