Bubble Sort Pseudocode and programming PDF

Title Bubble Sort Pseudocode and programming
Author Justice Williams Asare
Course Comparative Study of Programming Languages
Institution University of Energy and Natural Resources
Pages 3
File Size 57.2 KB
File Type PDF
Total Downloads 36
Total Views 160

Summary

Download Bubble Sort Pseudocode and programming PDF


Description

Bubble Sort Pseudo-code 1. Begin program 2. We are given with an input array which is supposed to be sorted in ascending order 3. We start with the first element and i=0 index and check if the element present at i+1 is greater then we swap the elements at index i and i+1. 4. If above is not the case, then no swapping will take place. 5. Now “ i ” gets incremented and the above 2 steps happen again until the array is exhausted. 6. We will ignore the last index as it is already sorted. 7. Now the largest element will be at the last index of the array. 8. Now we will again set i=0 and continue with the same steps that will eventually place second largest at second last place in the array. Now the last 2 indexes of the array are sorted. 9. End Program

Bubble Sort in Python def bubbleSort(array): length = len(array) for i in range(length-1): for j in range(0, length-i-1): if array[j] > array[j+1] : array[j], array[j+1] = array[j+1], array[j] arr = [10 7 8 9 1 5 ] print ("Elements of array before sorting:")

Elements of array before sorting: for i in range(len(arr)): print ("%d" %arr[i]), bubbleSort(arr) print ("Elements of array after sorting:") for i in range(len(arr)): print ("%d" %arr[i]), import time start_time = time.clock() print time.clock() - start_time, "seconds Execution Time"

Bubble Sort in JAVA class BubbleSort { void bubbleSort(int arr[]) { int size = arr.length;

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

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

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

int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } }

void display(int arr[]) { int size = arr.length; for (int i = 0; i < size; i++) System.out.println(arr[i]+" ");

} public static void main(String args[]) { int[] arr = { -2, 45, 0, 11, -9 }; BubbleSort bs = new BubbleSort(); bs.bubbleSort(arr); System.out.println("Sorted array::"); bs.display(arr); } }...


Similar Free PDFs