Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

add bubble sort algorithm using swift #78

Merged
merged 7 commits into from
Oct 20, 2021
Merged

add bubble sort algorithm using swift #78

merged 7 commits into from
Oct 20, 2021

Conversation

pubudu-m
Copy link
Contributor

Add bubble sort algorithm using Swift programming language #76
Please help to review. TIA!

Copy link
Member

@dilumdesilva dilumdesilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pubudu-m good work!

But I see some space to improve this version of the algorithm. You can introduce a shouldSwap sort of flag before entering the loop to skip unnecessary looping rounds as it increases the cost of your algorithm.

Ideally if shouldSwap logic is true only you should get into the loop and do the swap.

Here's a pseudo-code for what I suggest

Store data set
IF data_set.count < 1
Return data_set  
last_position <- data_set.count - 1
shouldSwap <- true
while shouldSwap = true
    shouldSwap <= false
    FOR i <- 0 to last position
        IF data_set[i] > data set[i + 1]
            temp <- data_set [i+ 1]
            data_set[i + 1] = data_set[I]
            data_set[i] <- temp
            shouldSwap <- true
        END IF
    ENDFOR
ENDWHILE

Please give it a look and update your PR if it helps to improve your algorithm.

Thanks.

@pubudu-m
Copy link
Contributor Author

pubudu-m commented Oct 19, 2021

Hi @dilum1995 updated the PR. Please help to review. TIA!

@dilumdesilva
Copy link
Member

@pubudu-m once you update this please mark the conversations as resolved as well.

Sorting Algorithms/Bubble Sort/BubbleSort.swift Outdated Show resolved Hide resolved
Sorting Algorithms/Bubble Sort/BubbleSort.swift Outdated Show resolved Hide resolved
Sorting Algorithms/Bubble Sort/BubbleSort.swift Outdated Show resolved Hide resolved
@dilumdesilva dilumdesilva merged commit a63a4eb into gcsrilanka:master Oct 20, 2021
@dilumdesilva dilumdesilva added the hacktoberfest-accepted Hacktoberfest Accepted label Oct 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hacktoberfest-accepted Hacktoberfest Accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants