SlideShare uma empresa Scribd logo
1 de 6
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

Mais conteúdo relacionado

Mais procurados

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topicSaddam Hussain
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operationsKamran Zafar
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting TechniquesRafay Farooq
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Linked list
Linked listLinked list
Linked listVONI
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
How bubble sort works
How bubble sort worksHow bubble sort works
How bubble sort worksGaurav Kumar
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptxsandeep54552
 

Mais procurados (20)

Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Heap sort
Heap sortHeap sort
Heap sort
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Linked list
Linked listLinked list
Linked list
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Stack application
Stack applicationStack application
Stack application
 
How bubble sort works
How bubble sort worksHow bubble sort works
How bubble sort works
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptx
 

Destaque

Bubble sort
Bubble sort Bubble sort
Bubble sort rmsz786
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort animFajar Zain
 
Binary code decimal Adder
Binary code decimal AdderBinary code decimal Adder
Binary code decimal AdderHaya Butt
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
3.4 selection sort
3.4 selection sort3.4 selection sort
3.4 selection sortKrish_ver2
 
Difference between various operating systems on the basis of single user ,mul...
Difference between various operating systems on the basis of single user ,mul...Difference between various operating systems on the basis of single user ,mul...
Difference between various operating systems on the basis of single user ,mul...Anu Garg
 

Destaque (20)

Bubble sort
Bubble sort Bubble sort
Bubble sort
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
The selection sort algorithm
The selection sort algorithmThe selection sort algorithm
The selection sort algorithm
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sorting
SortingSorting
Sorting
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
sort search in C
 sort search in C  sort search in C
sort search in C
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Linear and Bianry search
Linear and Bianry searchLinear and Bianry search
Linear and Bianry search
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort anim
 
Binary code decimal Adder
Binary code decimal AdderBinary code decimal Adder
Binary code decimal Adder
 
Selection sort
Selection sortSelection sort
Selection sort
 
3.4 selection sort
3.4 selection sort3.4 selection sort
3.4 selection sort
 
Difference between various operating systems on the basis of single user ,mul...
Difference between various operating systems on the basis of single user ,mul...Difference between various operating systems on the basis of single user ,mul...
Difference between various operating systems on the basis of single user ,mul...
 

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