1,079 questions
5
votes
1
answer
175
views
Why does std::is_sorted() change between calls with clang
I have some code which is showing odd behavior when std::is_sorted() is called twice in a row; the result changes! The fist call returns false and the second call returns true. This happens with the ...
-3
votes
1
answer
70
views
insertionsort different algorithm
I wanted to write insertionsort algorithm in a different way but I can not understand why my code does not work properly.
def insertionsort(x):
i=1
index=0
while(i>=1 and i<len(x)): #...
1
vote
1
answer
41
views
When get the maximum number of comparisons, in insertion-sort algorithm?
Examine the pseudo-code from the Wikipedia page on insertion sort:
i ← 1
while i < length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
end ...
2
votes
1
answer
76
views
is there any problem in the terms of time complexity in this code?
So today I was learning selection sort, and gain the concept how actually selection sort it working(i mean we add new element in the sorted array then check that element if it is on its correct ...
1
vote
1
answer
39
views
Insertion sort vs bubble sort vs selection sort: why is selection sort and insertion sort slower than bubbe sort although fewer steps?
I am implementing a script which compares bubble sort, insertion sort and selection sort.
Although all these have a time complexity of the order of N^2, selection sort is supposed to be twice as fast ...
0
votes
0
answers
11
views
Java FileWriter Not Creating Second Output File in Benchmark Program
I’m working on a Java benchmark program where I’m using FileWriter to write sorting algorithm results to text files. The program runs without any errors, and I’m able to create the first output file (...
0
votes
1
answer
67
views
Why is the cutoff value to insertion sort for small sub-arrays in optimizing quicksort algorithm is system-dependent?
In pp.296 Sedgewick & et al.'s Algorithm, 4rd edition, the author wrote:
The optimum value of the cutoff M is system-dependent, but any value
between 5 and 15 is likely to work well in most ...
-1
votes
1
answer
34
views
Binary insertion sort for a list of words
I am doing a enhanced insertion sort which is basically the same as binary insertion sort for a list of words called dictionary but for some reason my list is not getting sorted I cant seem to know ...
-1
votes
1
answer
86
views
What exactly does the while loop do here in insertion sort?
I'm looking at the insertion sort algorithm, which is correctly implemented with the below code, but I don't understand how the while loop does its job:
const arr = [8, 20, -2, 4, -6];
...
0
votes
1
answer
94
views
Which of the following insertion sort algorithms do you think is faster?
void insertion_sort_1(int *begin, int *end) {
for (int *cur = begin + 1; cur < end; ++cur) {
int tmp = *cur;
int *pos = cur;
for (int *i = cur; i > begin && *(...
-2
votes
3
answers
122
views
Insert Sort with Strings in C [closed]
I'm trying to make a insert sort with strings. That's my code below.
#include <stdio.h>
#include <string.h>
#define MAXLINES 5
#define MAXLINEC 1000
int
main(void)
{
char lines[...
-1
votes
2
answers
418
views
Time complexity of Insertion Sort Algorithm, if Array has 'I' number of inversions
I have recently been asked this question and was a little confused as I don't have a good grip on Sorting algorithms.
An array A contains 'I' number of inversions then what is the time
complexity of ...
0
votes
1
answer
132
views
Counting comparisons in C++ insertion sort
I am doing a simple insertion sort on an array with a swap helper function; I am trying to do a count of comparisons and swaps, and I was able to figure out the swap count, but I cannot figure out the ...
0
votes
0
answers
28
views
Why is my sorting algorithm not showing the desired output?
My question is with the counting. In my problem the number of the operations after sorting is not giving the same amount as it should. I am using the general logic for the selection sort, insertion ...
0
votes
1
answer
45
views
Time complexity of Insertion Sort of an array of n numbers, with additional information
Let there be an array A of n numbers.
Let's define an inversion: there are two indexes, i < j, such that A[i] > A[j].
If my array A has K inversions, what is the time complexity of sorting the ...