Created
October 27, 2015 15:56
-
-
Save tobiasstraub/3285373cc5d45a03d763 to your computer and use it in GitHub Desktop.
Example Implementation for optimized Bubblesort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Tobias Straub | |
* hello@tobiasstraub.com | |
* http://www.tobiasstraub.com | |
* | |
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. | |
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/. | |
*/ | |
package bubblesortversionelementsaving; | |
public class BubblesortVersionElementSaving { | |
public static void bubblesort(int[] dataSequence) { | |
int n = dataSequence.length; | |
int temp; | |
int elementSaving; | |
while (n > 1) { | |
elementSaving = 1; | |
for (int i = 0; i < n - 1; ++i) { | |
if (dataSequence[i + 1] < dataSequence[i]) { | |
temp = dataSequence[i]; | |
dataSequence[i] = dataSequence[i + 1]; | |
dataSequence[i + 1] = temp; | |
elementSaving = i + 1; | |
} | |
} | |
n = elementSaving; | |
} | |
} | |
public static void main(String[] args) { | |
int[] dataSequence = {6, 8, 10, 3}; // The data sequence to be sorted | |
bubblesort(dataSequence); // Call sort method | |
for (int i = 0; i < dataSequence.length; i++) { | |
System.out.print(dataSequence[i] + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment