SlideShare a Scribd company logo
2
Most read
6
Most read
BUBBLE SORT ALGORITHM 
LOGAN HARCH
BUBBLE SORT 
• WHAT IS IT? 
• WHAT DOES THE CODE LOOK LIKE? JAVA/PSEUDO 
• WHEN WOULD I NEED IT? 
• PRESENTATION OF WORKING CODE
WHAT IS IT? 
• IN THE BUBBLE SORT, AS ELEMENTS ARE SORTED THEY GRADUALLY "BUBBLE" (OR RISE) TO 
THEIR PROPER LOCATION IN THE ARRAY, LIKE BUBBLES RISING IN A GLASS OF SODA. THE 
BUBBLE SORT REPEATEDLY COMPARES ADJACENT ELEMENTS OF AN ARRAY. THE FIRST AND 
SECOND ELEMENTS ARE COMPARED AND SWAPPED IF OUT OF ORDER. THEN THE SECOND AND 
THIRD ELEMENTS ARE COMPARED AND SWAPPED IF OUT OF ORDER. THIS SORTING PROCESS 
CONTINUES UNTIL THE LAST TWO ELEMENTS OF THE ARRAY ARE COMPARED AND SWAPPED IF 
OUT OF ORDER. 
• WHEN THIS FIRST PASS THROUGH THE ARRAY IS COMPLETE, THE BUBBLE SORT RETURNS TO 
ELEMENTS ONE AND TWO AND STARTS THE PROCESS ALL OVER AGAIN. SO, WHEN DOES IT 
STOP? THE BUBBLE SORT KNOWS THAT IT IS FINISHED WHEN IT EXAMINES THE ENTIRE ARRAY 
AND NO "SWAPS" ARE NEEDED (THUS THE LIST IS IN PROPER ORDER). THE BUBBLE SORT KEEPS 
TRACK OF THE OCCURRING SWAPS BY THE USE OF A FLAG.
WHAT DOES THE CODE LOOK LIKE? 
• PUBLIC STATIC VOID BUBBLESORT( INT [ ] NUM ) 
{ 
INT J; 
BOOLEAN FLAG = TRUE; // SET FLAG TO TRUE TO BEGIN FIRST PASS 
INT TEMP; //HOLDING VARIABLE 
WHILE ( FLAG ) 
{ 
FLAG= FALSE; //SET FLAG TO FALSE AWAITING A POSSIBLE SWAP 
FOR( J=0; J < NUM.LENGTH -1; J++ ) 
{ 
IF ( NUM[ J ] < NUM[J+1] ) // CHANGE TO > FOR ASCENDING SORT 
{ 
TEMP = NUM[ J ]; //SWAP ELEMENTS 
NUM[ J ] = NUM[ J+1 ]; 
NUM[ J+1 ] = TEMP; 
FLAG = TRUE; //SHOWS A SWAP OCCURRED 
} 
} 
} 
}
WHAT DOES THE CODE LOOK LIKE? 
• BUBBLESORT( INT A[], INT N) 
BEGIN 
FOR I = 1 TO N-1 
SORTED = TRUE 
FOR J = 0 TO N-1-I 
IF A[J] > A[J+1] 
TEMP = A[J] 
A[J] = A[J+1] 
A[J+1] = TEMP 
SORTED = FALSE 
END FOR 
IF SORTED 
BREAK FROM I LOOP 
END FOR 
END
WHEN WOULD I NEED IT? 
• SORT NUMERICALLY 
• ASCENDING 
• DESCENDING 
• SORT ALPHABETICALLY 
• A-Z 
• Z-A

More Related Content

PPTX
Different types of Shoring Algorithms with Animation
PPTX
Binary search
PPTX
Bubble Sort Algorithm Presentation
PPTX
Searching & Sorting Algorithms
PPT
Bubble sort a best presentation topic
PPTX
Quicksort Presentation
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
Different types of Shoring Algorithms with Animation
Binary search
Bubble Sort Algorithm Presentation
Searching & Sorting Algorithms
Bubble sort a best presentation topic
Quicksort Presentation
358 33 powerpoint-slides_14-sorting_chapter-14

What's hot (20)

PPTX
Java presentation on insertion sort
PPT
Selection sort
PPTX
Insertion sort algorithm power point presentation
PPT
String matching algorithm
PPTX
Quick Sort
PPTX
Queue - Data Structure - Notes
PPTX
Insertion sort
PPTX
Selection sort
PPTX
SORTING techniques.pptx
PPT
Selection sort
PPTX
Stack_Application_Infix_Prefix.pptx
PPT
Algorithm: Quick-Sort
PPTX
Merge sort and quick sort
PDF
Binary Search - Design & Analysis of Algorithms
PPSX
Algorithm and Programming (Searching)
PPT
Priority queues
PDF
Queues
PPT
Sorting Techniques
PPTX
Queue Implementation Using Array & Linked List
Java presentation on insertion sort
Selection sort
Insertion sort algorithm power point presentation
String matching algorithm
Quick Sort
Queue - Data Structure - Notes
Insertion sort
Selection sort
SORTING techniques.pptx
Selection sort
Stack_Application_Infix_Prefix.pptx
Algorithm: Quick-Sort
Merge sort and quick sort
Binary Search - Design & Analysis of Algorithms
Algorithm and Programming (Searching)
Priority queues
Queues
Sorting Techniques
Queue Implementation Using Array & Linked List
Ad

Viewers also liked (20)

PPTX
Bubble Sort
PDF
Bubblesort Algorithm
PPT
3.1 bubble sort
PPT
Bubble sort
PPT
DATA STRUCTURES
PPTX
The selection sort algorithm
PPTX
Selection sort
PPT
Sorting
PPTX
Sorting algorithms
PPT
Sorting Algorithms
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Data Structures - Searching & sorting
PPT
Bubble sort
PPTX
Sorting
PPT
Bubble sort
PPT
Bubble sort
PDF
sort search in C
PDF
PDF
Sorting
PPT
Linear and Bianry search
Bubble Sort
Bubblesort Algorithm
3.1 bubble sort
Bubble sort
DATA STRUCTURES
The selection sort algorithm
Selection sort
Sorting
Sorting algorithms
Sorting Algorithms
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Searching & sorting
Bubble sort
Sorting
Bubble sort
Bubble sort
sort search in C
Sorting
Linear and Bianry search
Ad

Bubble sort algorithm

  • 1. BUBBLE SORT ALGORITHM LOGAN HARCH
  • 2. BUBBLE SORT • WHAT IS IT? • WHAT DOES THE CODE LOOK LIKE? JAVA/PSEUDO • WHEN WOULD I NEED IT? • PRESENTATION OF WORKING CODE
  • 3. WHAT IS IT? • IN THE BUBBLE SORT, AS ELEMENTS ARE SORTED THEY GRADUALLY "BUBBLE" (OR RISE) TO THEIR PROPER LOCATION IN THE ARRAY, LIKE BUBBLES RISING IN A GLASS OF SODA. THE BUBBLE SORT REPEATEDLY COMPARES ADJACENT ELEMENTS OF AN ARRAY. THE FIRST AND SECOND ELEMENTS ARE COMPARED AND SWAPPED IF OUT OF ORDER. THEN THE SECOND AND THIRD ELEMENTS ARE COMPARED AND SWAPPED IF OUT OF ORDER. THIS SORTING PROCESS CONTINUES UNTIL THE LAST TWO ELEMENTS OF THE ARRAY ARE COMPARED AND SWAPPED IF OUT OF ORDER. • WHEN THIS FIRST PASS THROUGH THE ARRAY IS COMPLETE, THE BUBBLE SORT RETURNS TO ELEMENTS ONE AND TWO AND STARTS THE PROCESS ALL OVER AGAIN. SO, WHEN DOES IT STOP? THE BUBBLE SORT KNOWS THAT IT IS FINISHED WHEN IT EXAMINES THE ENTIRE ARRAY AND NO "SWAPS" ARE NEEDED (THUS THE LIST IS IN PROPER ORDER). THE BUBBLE SORT KEEPS TRACK OF THE OCCURRING SWAPS BY THE USE OF A FLAG.
  • 4. WHAT DOES THE CODE LOOK LIKE? • PUBLIC STATIC VOID BUBBLESORT( INT [ ] NUM ) { INT J; BOOLEAN FLAG = TRUE; // SET FLAG TO TRUE TO BEGIN FIRST PASS INT TEMP; //HOLDING VARIABLE WHILE ( FLAG ) { FLAG= FALSE; //SET FLAG TO FALSE AWAITING A POSSIBLE SWAP FOR( J=0; J < NUM.LENGTH -1; J++ ) { IF ( NUM[ J ] < NUM[J+1] ) // CHANGE TO > FOR ASCENDING SORT { TEMP = NUM[ J ]; //SWAP ELEMENTS NUM[ J ] = NUM[ J+1 ]; NUM[ J+1 ] = TEMP; FLAG = TRUE; //SHOWS A SWAP OCCURRED } } } }
  • 5. WHAT DOES THE CODE LOOK LIKE? • BUBBLESORT( INT A[], INT N) BEGIN FOR I = 1 TO N-1 SORTED = TRUE FOR J = 0 TO N-1-I IF A[J] > A[J+1] TEMP = A[J] A[J] = A[J+1] A[J+1] = TEMP SORTED = FALSE END FOR IF SORTED BREAK FROM I LOOP END FOR END
  • 6. WHEN WOULD I NEED IT? • SORT NUMERICALLY • ASCENDING • DESCENDING • SORT ALPHABETICALLY • A-Z • Z-A