Insertion sort in python with negative numbers at the end of the list
I used the sorting algorithm to sort numbers like positives first and then negatives (swapping in place, you can use variables but without using any container variables. Just assume we don't have more memory space for extra containers). So the sorted positive numbers first, then the sorted negatives.
I did it, but I needed another for
loop for the negatives to sort and insert them at the end of the list.
My question: is there a way to modify the first loop to achieve the same result?
(ie: use only 1 for
loop)
for example:
x = [9, -3, 2, -7, -4, -1, 5, 4]
The output should be:
[2, 4, 5, 9, -7, -4, -3, -1]
Here is my solution (but I used 2 for
loops, and I want to only use 1):
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] < 0:
if data[e] < 0:
if data[e - 1] < data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
python python-3.x sorting
add a comment |
I used the sorting algorithm to sort numbers like positives first and then negatives (swapping in place, you can use variables but without using any container variables. Just assume we don't have more memory space for extra containers). So the sorted positive numbers first, then the sorted negatives.
I did it, but I needed another for
loop for the negatives to sort and insert them at the end of the list.
My question: is there a way to modify the first loop to achieve the same result?
(ie: use only 1 for
loop)
for example:
x = [9, -3, 2, -7, -4, -1, 5, 4]
The output should be:
[2, 4, 5, 9, -7, -4, -3, -1]
Here is my solution (but I used 2 for
loops, and I want to only use 1):
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] < 0:
if data[e] < 0:
if data[e - 1] < data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
python python-3.x sorting
1
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50
add a comment |
I used the sorting algorithm to sort numbers like positives first and then negatives (swapping in place, you can use variables but without using any container variables. Just assume we don't have more memory space for extra containers). So the sorted positive numbers first, then the sorted negatives.
I did it, but I needed another for
loop for the negatives to sort and insert them at the end of the list.
My question: is there a way to modify the first loop to achieve the same result?
(ie: use only 1 for
loop)
for example:
x = [9, -3, 2, -7, -4, -1, 5, 4]
The output should be:
[2, 4, 5, 9, -7, -4, -3, -1]
Here is my solution (but I used 2 for
loops, and I want to only use 1):
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] < 0:
if data[e] < 0:
if data[e - 1] < data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
python python-3.x sorting
I used the sorting algorithm to sort numbers like positives first and then negatives (swapping in place, you can use variables but without using any container variables. Just assume we don't have more memory space for extra containers). So the sorted positive numbers first, then the sorted negatives.
I did it, but I needed another for
loop for the negatives to sort and insert them at the end of the list.
My question: is there a way to modify the first loop to achieve the same result?
(ie: use only 1 for
loop)
for example:
x = [9, -3, 2, -7, -4, -1, 5, 4]
The output should be:
[2, 4, 5, 9, -7, -4, -3, -1]
Here is my solution (but I used 2 for
loops, and I want to only use 1):
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] < 0:
if data[e] < 0:
if data[e - 1] < data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
python python-3.x sorting
python python-3.x sorting
edited Nov 19 '18 at 16:29
asked Nov 19 '18 at 16:05
FouadDev
586
586
1
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50
add a comment |
1
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50
1
1
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50
add a comment |
2 Answers
2
active
oldest
votes
You can define a function is_greater
as follows:
def is_greater(x, y):
if (x < 0 and y < 0) or (x >= 0 and y >= 0):
return x > y
return x < 0
Then you can use this instead of normal int comparison in your first loop:
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and is_greater(data[e - 1], data[e]):
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
insertion_sort(d)
Output
[2, 4, 5, 9, -7, -4, -3, -1]
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
add a comment |
You just need to find the pivot value when the numbers start to be positives
def insertion_sort(data):
pivot = -1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
if data[e] >= 0 and data[e-1] < 0:
pivot = e
e -= 1
data[-pivot:], data[:-pivot] = data[:pivot], data[pivot:]
print(data)
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
|
show 1 more comment
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53378534%2finsertion-sort-in-python-with-negative-numbers-at-the-end-of-the-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can define a function is_greater
as follows:
def is_greater(x, y):
if (x < 0 and y < 0) or (x >= 0 and y >= 0):
return x > y
return x < 0
Then you can use this instead of normal int comparison in your first loop:
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and is_greater(data[e - 1], data[e]):
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
insertion_sort(d)
Output
[2, 4, 5, 9, -7, -4, -3, -1]
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
add a comment |
You can define a function is_greater
as follows:
def is_greater(x, y):
if (x < 0 and y < 0) or (x >= 0 and y >= 0):
return x > y
return x < 0
Then you can use this instead of normal int comparison in your first loop:
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and is_greater(data[e - 1], data[e]):
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
insertion_sort(d)
Output
[2, 4, 5, 9, -7, -4, -3, -1]
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
add a comment |
You can define a function is_greater
as follows:
def is_greater(x, y):
if (x < 0 and y < 0) or (x >= 0 and y >= 0):
return x > y
return x < 0
Then you can use this instead of normal int comparison in your first loop:
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and is_greater(data[e - 1], data[e]):
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
insertion_sort(d)
Output
[2, 4, 5, 9, -7, -4, -3, -1]
You can define a function is_greater
as follows:
def is_greater(x, y):
if (x < 0 and y < 0) or (x >= 0 and y >= 0):
return x > y
return x < 0
Then you can use this instead of normal int comparison in your first loop:
def insertion_sort(data):
for i in range(1, len(data)):
e = i
while e > 0 and is_greater(data[e - 1], data[e]):
data[e - 1], data[e] = data[e], data[e - 1]
e -= 1
print(data)
insertion_sort(d)
Output
[2, 4, 5, 9, -7, -4, -3, -1]
answered Nov 19 '18 at 16:21
slider
8,08011129
8,08011129
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
add a comment |
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
1
1
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
Yup, thanks @slider, that's a good way to go about it. You didn't use any extra container (iterable). You only swapped them in place. good solution
– FouadDev
Nov 19 '18 at 16:35
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
But I wish I could find a solution without extra function. Just like modifying that only for loop to do the job
– FouadDev
Nov 19 '18 at 16:47
add a comment |
You just need to find the pivot value when the numbers start to be positives
def insertion_sort(data):
pivot = -1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
if data[e] >= 0 and data[e-1] < 0:
pivot = e
e -= 1
data[-pivot:], data[:-pivot] = data[:pivot], data[pivot:]
print(data)
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
|
show 1 more comment
You just need to find the pivot value when the numbers start to be positives
def insertion_sort(data):
pivot = -1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
if data[e] >= 0 and data[e-1] < 0:
pivot = e
e -= 1
data[-pivot:], data[:-pivot] = data[:pivot], data[pivot:]
print(data)
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
|
show 1 more comment
You just need to find the pivot value when the numbers start to be positives
def insertion_sort(data):
pivot = -1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
if data[e] >= 0 and data[e-1] < 0:
pivot = e
e -= 1
data[-pivot:], data[:-pivot] = data[:pivot], data[pivot:]
print(data)
You just need to find the pivot value when the numbers start to be positives
def insertion_sort(data):
pivot = -1
for i in range(1, len(data)):
e = i
while e > 0 and data[e - 1] > data[e]:
data[e - 1], data[e] = data[e], data[e - 1]
if data[e] >= 0 and data[e-1] < 0:
pivot = e
e -= 1
data[-pivot:], data[:-pivot] = data[:pivot], data[pivot:]
print(data)
edited Nov 19 '18 at 16:45
answered Nov 19 '18 at 16:17
Hemerson Tacon
9021315
9021315
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
|
show 1 more comment
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
Thanks @Hemerson Tacon but you didn't meet my requirements. I said to to use any extra container variable, but you used neg variable that has a list value. I want a solution that just swap elements "in place". But thanks for trying though
– FouadDev
Nov 19 '18 at 16:36
1
1
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
@FouadDev just fixed the code, it had some errors, now I'm not using extra container anymore
– Hemerson Tacon
Nov 19 '18 at 16:41
1
1
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Since negative values index backwards from the end of a list, that last line can be simplified.
– trentcl
Nov 19 '18 at 16:43
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
Thanks @trentcl ! Improved code with this.
– Hemerson Tacon
Nov 19 '18 at 16:45
1
1
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
Yes @Hemerson Tacon it's actually a good solution. Great job man
– FouadDev
Nov 19 '18 at 16:53
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53378534%2finsertion-sort-in-python-with-negative-numbers-at-the-end-of-the-list%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
1
Have you considered doing a normal insertion sort, then finding the index at which your list changes from negative to positive, and just re-slicing the list? The algorithm would still be bounded by the sort.
– user3483203
Nov 19 '18 at 16:13
Yes the idea is good, but re-slicing the list means using extra lists (I said in my question to not use any extra container, just swap elements in place)
– FouadDev
Nov 19 '18 at 16:50