In place Quick Sort implementation using bitwise swap
Do I really need to have a scientific explanation for my premise?
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
A three room house but a three headED dog
Accepted offer letter, position changed
Examples of a statistic that is not independent of sample's distribution?
htop displays identical program in multiple lines
Why would one plane in this picture not have gear down yet?
How to pass a string to a command that expects a file?
Is there an elementary proof that there are infinitely many primes that are *not* completely split in an abelian extension?
Low budget alien movie about the Earth being cooked
Should I take out a loan for a friend to invest on my behalf?
Accountant/ lawyer will not return my call
Should QA ask requirements to developers?
Could you please stop shuffling the deck and play already?
How strictly should I take "Candidates must be local"?
Why is Beresheet doing a only a one-way trip?
Algorithm to convert a fixed-length string to the smallest possible collision-free representation?
How do I deal with a powergamer in a game full of beginners in a school club?
Why don't MCU characters ever seem to have language issues?
Who is eating data? Xargs?
Can Mathematica be used to create an Artistic 3D extrusion from a 2D image and wrap a line pattern around it?
How can I budget to build up a down payment for a house over the course of a year?
Word for a person who has no opinion about whether god exists
How much stiffer are 23c tires over 28c?
In place Quick Sort implementation using bitwise swap
$begingroup$
I have a problem with the implementation logic and more specifically a bug when using bitwise swap instead of a temp variable.
I don't seem to figure out the reason.
Can you suggest any possible optimization for an in place quick sort, as well. Here is the code (in C#):
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 36, 10, 1, 34, 28, 38, 31, 27, 30, 20 };
// ARRAY QUICK SORT IN PLACE
Console.WriteLine("Pre-sortede:");
ArrayPrinter(arr);
QuickSort(arr);
Console.WriteLine("Sorted using Merge Sort:");
ArrayPrinter(arr);
}
static void ArrayPrinter(int[] array)
{
foreach (var i in array)
{
Console.WriteLine(i);
}
}
//Helper Method
public static void QuickSort(int[] array)
{
//starting and ending range to sort
QuickSort(array, 0, array.Length - 1);
}
// Main Quick Sort
public static void QuickSort(int[] array, int low, int high)
{
if (low < high)
{
var pivotIndex = Partition(array, low, high);
QuickSort(array, low, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, high);
}
}
// Partitions
public static int Partition(int[] array, int low, int high)
{
int pivot = array[high];
int indexer = low - 1; // pre-set -1 #1
for (int i = low; i < high; i++)
{
if (array[i] <= pivot)
{
++indexer;
int temp = array[indexer];
array[indexer] = array[i];
array[i] = temp;
// [DOESNT!]??????????? => 0s out
// array[i] ^= array[indexer];
// array[indexer] ^= array[i];
// array[i] ^= array[indexer];
}
}
++indexer;
//[WORKS]
array[high] ^= array[indexer];
array[indexer] ^= array[high];
array[high] ^= array[indexer];
// var temp1 = array[indexer];
// array[indexer] = array[high];
// array[high] = temp1;
return indexer;
}
}
Why is this part:
array[i] ^= array[indexer];
array[indexer] ^= array[i];
array[i] ^= array[indexer];
making such a mess
c# algorithm quick-sort
New contributor
$endgroup$
add a comment |
$begingroup$
I have a problem with the implementation logic and more specifically a bug when using bitwise swap instead of a temp variable.
I don't seem to figure out the reason.
Can you suggest any possible optimization for an in place quick sort, as well. Here is the code (in C#):
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 36, 10, 1, 34, 28, 38, 31, 27, 30, 20 };
// ARRAY QUICK SORT IN PLACE
Console.WriteLine("Pre-sortede:");
ArrayPrinter(arr);
QuickSort(arr);
Console.WriteLine("Sorted using Merge Sort:");
ArrayPrinter(arr);
}
static void ArrayPrinter(int[] array)
{
foreach (var i in array)
{
Console.WriteLine(i);
}
}
//Helper Method
public static void QuickSort(int[] array)
{
//starting and ending range to sort
QuickSort(array, 0, array.Length - 1);
}
// Main Quick Sort
public static void QuickSort(int[] array, int low, int high)
{
if (low < high)
{
var pivotIndex = Partition(array, low, high);
QuickSort(array, low, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, high);
}
}
// Partitions
public static int Partition(int[] array, int low, int high)
{
int pivot = array[high];
int indexer = low - 1; // pre-set -1 #1
for (int i = low; i < high; i++)
{
if (array[i] <= pivot)
{
++indexer;
int temp = array[indexer];
array[indexer] = array[i];
array[i] = temp;
// [DOESNT!]??????????? => 0s out
// array[i] ^= array[indexer];
// array[indexer] ^= array[i];
// array[i] ^= array[indexer];
}
}
++indexer;
//[WORKS]
array[high] ^= array[indexer];
array[indexer] ^= array[high];
array[high] ^= array[indexer];
// var temp1 = array[indexer];
// array[indexer] = array[high];
// array[high] = temp1;
return indexer;
}
}
Why is this part:
array[i] ^= array[indexer];
array[indexer] ^= array[i];
array[i] ^= array[indexer];
making such a mess
c# algorithm quick-sort
New contributor
$endgroup$
add a comment |
$begingroup$
I have a problem with the implementation logic and more specifically a bug when using bitwise swap instead of a temp variable.
I don't seem to figure out the reason.
Can you suggest any possible optimization for an in place quick sort, as well. Here is the code (in C#):
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 36, 10, 1, 34, 28, 38, 31, 27, 30, 20 };
// ARRAY QUICK SORT IN PLACE
Console.WriteLine("Pre-sortede:");
ArrayPrinter(arr);
QuickSort(arr);
Console.WriteLine("Sorted using Merge Sort:");
ArrayPrinter(arr);
}
static void ArrayPrinter(int[] array)
{
foreach (var i in array)
{
Console.WriteLine(i);
}
}
//Helper Method
public static void QuickSort(int[] array)
{
//starting and ending range to sort
QuickSort(array, 0, array.Length - 1);
}
// Main Quick Sort
public static void QuickSort(int[] array, int low, int high)
{
if (low < high)
{
var pivotIndex = Partition(array, low, high);
QuickSort(array, low, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, high);
}
}
// Partitions
public static int Partition(int[] array, int low, int high)
{
int pivot = array[high];
int indexer = low - 1; // pre-set -1 #1
for (int i = low; i < high; i++)
{
if (array[i] <= pivot)
{
++indexer;
int temp = array[indexer];
array[indexer] = array[i];
array[i] = temp;
// [DOESNT!]??????????? => 0s out
// array[i] ^= array[indexer];
// array[indexer] ^= array[i];
// array[i] ^= array[indexer];
}
}
++indexer;
//[WORKS]
array[high] ^= array[indexer];
array[indexer] ^= array[high];
array[high] ^= array[indexer];
// var temp1 = array[indexer];
// array[indexer] = array[high];
// array[high] = temp1;
return indexer;
}
}
Why is this part:
array[i] ^= array[indexer];
array[indexer] ^= array[i];
array[i] ^= array[indexer];
making such a mess
c# algorithm quick-sort
New contributor
$endgroup$
I have a problem with the implementation logic and more specifically a bug when using bitwise swap instead of a temp variable.
I don't seem to figure out the reason.
Can you suggest any possible optimization for an in place quick sort, as well. Here is the code (in C#):
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 36, 10, 1, 34, 28, 38, 31, 27, 30, 20 };
// ARRAY QUICK SORT IN PLACE
Console.WriteLine("Pre-sortede:");
ArrayPrinter(arr);
QuickSort(arr);
Console.WriteLine("Sorted using Merge Sort:");
ArrayPrinter(arr);
}
static void ArrayPrinter(int[] array)
{
foreach (var i in array)
{
Console.WriteLine(i);
}
}
//Helper Method
public static void QuickSort(int[] array)
{
//starting and ending range to sort
QuickSort(array, 0, array.Length - 1);
}
// Main Quick Sort
public static void QuickSort(int[] array, int low, int high)
{
if (low < high)
{
var pivotIndex = Partition(array, low, high);
QuickSort(array, low, pivotIndex - 1);
QuickSort(array, pivotIndex + 1, high);
}
}
// Partitions
public static int Partition(int[] array, int low, int high)
{
int pivot = array[high];
int indexer = low - 1; // pre-set -1 #1
for (int i = low; i < high; i++)
{
if (array[i] <= pivot)
{
++indexer;
int temp = array[indexer];
array[indexer] = array[i];
array[i] = temp;
// [DOESNT!]??????????? => 0s out
// array[i] ^= array[indexer];
// array[indexer] ^= array[i];
// array[i] ^= array[indexer];
}
}
++indexer;
//[WORKS]
array[high] ^= array[indexer];
array[indexer] ^= array[high];
array[high] ^= array[indexer];
// var temp1 = array[indexer];
// array[indexer] = array[high];
// array[high] = temp1;
return indexer;
}
}
Why is this part:
array[i] ^= array[indexer];
array[indexer] ^= array[i];
array[i] ^= array[indexer];
making such a mess
c# algorithm quick-sort
c# algorithm quick-sort
New contributor
New contributor
New contributor
asked 2 mins ago
VasilVasil
11
11
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Vasil is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215289%2fin-place-quick-sort-implementation-using-bitwise-swap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Vasil is a new contributor. Be nice, and check out our Code of Conduct.
Vasil is a new contributor. Be nice, and check out our Code of Conduct.
Vasil is a new contributor. Be nice, and check out our Code of Conduct.
Vasil is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215289%2fin-place-quick-sort-implementation-using-bitwise-swap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown