- Notifications
You must be signed in to change notification settings - Fork180
Binary sort#26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
bhadrik wants to merge2 commits intoalgorithm-visualizer:masterChoose a base branch frombhadrik:Binary-Sort
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Binary sort#26
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletionsDivide and Conquer/Binary Sort/README.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# BinarySort | ||
Binary sort is an algorithm that sorts the given array of numbers based on its binary value. | ||
# How to use | ||
1. First compile both C file using following command: "gcc -o BinarySort BinarySort.c" & "gcc -o Generator Generator.c" | ||
2. Then run "Generator.exe" file using following command: "Generator.exe 10000 Data.txt" - Here 10000 is size of array which is going to be generated. | ||
3. It will generate Data.txt file, filled with 10000 random numbers. | ||
4. Then run "BinarySort.exe" file using following command: "BinarySort.exe 10000 Data.txt" for windows & "./BinarySort 10000 Data.txt" for linux | ||
5. It will use Data.txt file as an input to the algorithm and sort thos data and will generate "Sorted Data.txt" file which is sorted output. | ||
# Working of an algorithm | ||
* This algorithm is based on binary value of the numbers. | ||
``` | ||
8 - 1000 | ||
5 - 0011 | ||
6 - 0110 | ||
4th bit 4th bit(R) 4th bit(R) 3rd bit(L) 3rd bit(L) | ||
+-----------+ +-------+ +---+ +-------+ +---+ +-------+ +---+ +---+ +---+ +---+ Everything is fixed | ||
| 8 | 5 | 6 | -> | 6 | 5 | | 8 | -> | 6 | 5 | | 8 | -> | 6 | 5 | | 8 | -> | 5 | | 6 | | 8 | -> so arry is solved | ||
+-----------+ +-------+ +---+ +-------+ +---+ +-------+ +---+ +---+ +---+ +---+ easily without | ||
L U L U U=L L U Fix L U Fix Fix Fix Fix comparing single no. | ||
Input array Found 4th bit of Recursion call Recusion call for Found 3rd bit of | ||
8 is *1* so swap right array is left array for 6 is *1* so swap | ||
with number at U sorted. *bitNo* 3. with number at U | ||
and devide the and decide the | ||
array. array. | ||
``` |
105 changes: 105 additions & 0 deletionsDivide and Conquer/Binary Sort/code.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import org.algorithm_visualizer.*; | ||
import java.util.Arrays; | ||
public class Main { | ||
private static ChartTracer chartTracer = new ChartTracer("Chart"); | ||
private static LogTracer logTracer = new LogTracer("Console"); | ||
private static Integer [] data = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create(); | ||
public static void main(String[] args) { | ||
int length = data.length, bitNo = 6,lowerBound = 0; | ||
int upperBound = length - 1; | ||
logTracer.printf("original data = %s\n",Arrays.toString(data)); | ||
chartTracer.set(data); | ||
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, logTracer})); | ||
Tracer.delay(); | ||
// binaryOfAllNumbers(); | ||
binarySort(data, bitNo-1, lowerBound, upperBound); | ||
logTracer.printf("sorted data = %s\n",Arrays.toString(data)); | ||
} | ||
private static void binarySort(Integer [] data, int bitNo, int lowerBound, int upperBound) { | ||
logTracer.reset(); | ||
logTracer.printf("scanning bitNo:%s for (%s, %s)\n", bitNo, lowerBound, upperBound); | ||
chartTracer.set(data); | ||
chartTracer.select(lowerBound, upperBound); | ||
Tracer.delay(); | ||
int l_backup = lowerBound, u_backup = upperBound, i; | ||
boolean letsGo = false; | ||
do{ | ||
//Scaning all the numbers between location "lowerBound" to "upperBound" | ||
for (i = l_backup; i <= upperBound; i++) { | ||
int temp = (data[i] >> bitNo) & 1; | ||
boolean condition; | ||
if(temp >= 1){ | ||
condition = true; | ||
} | ||
else{ | ||
condition = false; | ||
} | ||
if (condition) { | ||
letsGo = true; | ||
if (i != upperBound) { | ||
logTracer.printf("swap %s and %s\n",data[i],data[upperBound]); | ||
swap(data, i, upperBound); | ||
chartTracer.set(data); | ||
chartTracer.select(lowerBound, upperBound); | ||
Tracer.delay(); | ||
i--; | ||
} | ||
chartTracer.deselect(upperBound); | ||
upperBound--; | ||
} | ||
} | ||
bitNo--; | ||
}while(!letsGo && bitNo > 0); | ||
bitNo++; | ||
int hold = upperBound; | ||
if (upperBound == u_backup){ | ||
hold = lowerBound; | ||
} | ||
else{ | ||
hold = upperBound + 1; | ||
} | ||
//Again calling for divided subarry, and now for (bitNo - 1)th bit | ||
if(bitNo > 0 && (hold != u_backup || lowerBound != upperBound)){ | ||
binarySort(data, (bitNo - 1), hold, u_backup); | ||
binarySort(data, (bitNo - 1), lowerBound, upperBound); | ||
} | ||
} | ||
private static void swap(Integer [] data, int x, int y) { | ||
int temp = data[x]; | ||
data[x] = data[y]; | ||
data[y] = temp; | ||
chartTracer.patch(x, data[x]); | ||
chartTracer.patch(y, data[y]); | ||
Tracer.delay(); | ||
chartTracer.depatch(x); | ||
chartTracer.depatch(y); | ||
} | ||
private static void binaryOfAllNumbers(){ | ||
for(int i=0; i<data.length; i++){ | ||
logTracer.printf("%s [",data[i]); | ||
for(int bit=5; bit>=0; bit--){ | ||
logTracer.printf(" %s",(data[i] >> bit) & 1); | ||
} | ||
logTracer.printf("]\n"); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.