473,511 Members | 16,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

an implementation of insertion sort, insert without move element

Suppose we have an array a[N], the idea is: build another array, int
next[N], for each 0<i<N, next[i] = next position of a[i] in the sorted
array, if a[i] is the max, then next[i] is undefined. For example, let
the unsorted array is
a[] = {5, 4, 2, 1, 3};
then
next[]
would be
{undefined, 0, 4, 2, 1}
after sorted.
The process of sort is the process of building array next[]. Finally,
build sorted a[] according to
next[]. But at the last step, I cannot build a[] according to next[]
directly, so I have to build another array tmp[N] to store the sorted
sorted result, then copy it back to array a[], but I believe there is
some way to do it. Does anyone who does well in algo analysis can
help me to analyze my algo$B!)(BI want to know if my algo is more efficient
than the original one. And is there any way to build a[] directly at
the last step? Thanks!
The following is my code, we assume that the element type is int.
#include<stdio.h>
void myInsertionSort(int a[], int size)
{
int *next, *tmp;
next = malloc(size * sizeof(int));
tmp = malloc(size * sizeof(int));
int first = 0, last = 0, max;
int i;
for(i = 1, max = a[0]; i < size; i++){
if(a[i] < max){
if(a[i] <= a[first]){
next[i] = first;
first = i;
}else{
int j = first;
while(a[i] a[next[j]])
j = next[j];
next[i] = next[j];
next[j] = i;
}
}else{
next[last] = i;
max = a[i];
last = i;
}
}
int j;
for(i = 0, j = first; i < size; i++){
tmp[i] = a[j];
j = next[j];
}
for(i = 0; i < size; i++)
a[i] = tmp[i];
free(next);
free(tmp);
}
int main()
{
int a[] = {3, 9, 6, 4, 1, 5, 7, 2, 10, 8, 87, 95, 456, 127, 54,
784, 65, 313, 75};
#define ARRAY_LENTH(array) (sizeof ((array)) / sizeof(int))
myInsertionSort(a, ARRAY_LENTH(a));
int i;
for(i = 0; i < ARRAY_LENTH(a); i++)
printf("%d, ", a[i]);
return 0;
}
Jul 25 '08 #1
2 2841
Gestorm wrote:
Suppose we have an array a[N], the idea is: build another array, int
next[N], for each 0<i<N, next[i] = next position of a[i] in the sorted
array, if a[i] is the max, then next[i] is undefined. For example, let
the unsorted array is
a[] = {5, 4, 2, 1, 3};
then
next[]
would be
{undefined, 0, 4, 2, 1}
after sorted.
The process of sort is the process of building array next[]. Finally,
build sorted a[] according to
next[]. But at the last step, I cannot build a[] according to next[]
directly, so I have to build another array tmp[N] to store the sorted
sorted result, then copy it back to array a[], but I believe there is
some way to do it. Does anyone who does well in algo analysis can
help me to analyze my algo$B!)(BI want to know if my algo is more efficient
than the original one. And is there any way to build a[] directly at
the last step? Thanks!
The following is my code, we assume that the element type is int.
#include<stdio.h>
void myInsertionSort(int a[], int size)
{
int *next, *tmp;
next = malloc(size * sizeof(int));
tmp = malloc(size * sizeof(int));
int first = 0, last = 0, max;
int i;
for(i = 1, max = a[0]; i < size; i++){
if(a[i] < max){
if(a[i] <= a[first]){
next[i] = first;
first = i;
}else{
int j = first;
while(a[i] a[next[j]])
j = next[j];
next[i] = next[j];
next[j] = i;
}
}else{
next[last] = i;
max = a[i];
last = i;
}
}
int j;
for(i = 0, j = first; i < size; i++){
tmp[i] = a[j];
j = next[j];
}
for(i = 0; i < size; i++)
a[i] = tmp[i];
free(next);
free(tmp);
}
int main()
{
int a[] = {3, 9, 6, 4, 1, 5, 7, 2, 10, 8, 87, 95, 456, 127, 54,
784, 65, 313, 75};
#define ARRAY_LENTH(array) (sizeof ((array)) / sizeof(int))
myInsertionSort(a, ARRAY_LENTH(a));
int i;
for(i = 0; i < ARRAY_LENTH(a); i++)
printf("%d, ", a[i]);
return 0;
}
There is a way to do what you want,
but as far as I can guess, it's an O(N * N) operation,
at which point I start to lose interest in working out the details.

To do the equivalent of what your code does, using qsort,
I would make a copy of the original array and then
I would make an array of pointers to the copy array elements,
and then sort the array of pointers,
and then use that array to rewrite the original array.

To do it with only (N + 1) element copy operations,
you would need a temp buffer for a single array element.
You would copy the original value
of the first element to be overwritten
to the buffer.
You would then need to follow a list
of source and destination elements,
so that whichever element was the source
in the last write operation, would become
the destination for the next write operation.
I think that working out that list,
would be an O(N*N) operation.

--
pete
Jul 26 '08 #2
Gestorm wrote:
Suppose we have an array a[N], the idea is: build another array, int
next[N], for each 0<i<N, next[i] = next position of a[i] in the sorted
array, if a[i] is the max, then next[i] is undefined. For example, let
the unsorted array is
a[] = {5, 4, 2, 1, 3};
then
next[]
would be
{undefined, 0, 4, 2, 1}
after sorted.
The process of sort is the process of building array next[]. Finally,
build sorted a[] according to
next[]. But at the last step, I cannot build a[] according to next[]
directly, so I have to build another array tmp[N] to store the sorted
sorted result, then copy it back to array a[], but I believe there is
some way to do it. Does anyone who does well in algo analysis can
help me to analyze my algo$B!)(BI want to know if my algo is more efficient
than the original one. And is there any way to build a[] directly at
the last step? Thanks!
The following is my code, we assume that the element type is int.
#include<stdio.h>
void myInsertionSort(int a[], int size)
{
int *next, *tmp;
next = malloc(size * sizeof(int));
tmp = malloc(size * sizeof(int));
int first = 0, last = 0, max;
int i;
for(i = 1, max = a[0]; i < size; i++){
if(a[i] < max){
if(a[i] <= a[first]){
next[i] = first;
first = i;
}else{
int j = first;
while(a[i] a[next[j]])
j = next[j];
next[i] = next[j];
next[j] = i;
}
}else{
next[last] = i;
max = a[i];
last = i;
}
}
int j;
for(i = 0, j = first; i < size; i++){
tmp[i] = a[j];
j = next[j];
}
for(i = 0; i < size; i++)
a[i] = tmp[i];
free(next);
free(tmp);
}
int main()
{
int a[] = {3, 9, 6, 4, 1, 5, 7, 2, 10, 8, 87, 95, 456, 127, 54,
784, 65, 313, 75};
#define ARRAY_LENTH(array) (sizeof ((array)) / sizeof(int))
myInsertionSort(a, ARRAY_LENTH(a));
int i;
for(i = 0; i < ARRAY_LENTH(a); i++)
printf("%d, ", a[i]);
return 0;
}
Much better than a sort function definition
where everything is type int, like this:

void myInsertionSort(int a[], int size);

is one in which the indexes and the element types
are clearly distinct, and in which macros are used
for operations between array elements, like this:

#define E_TYPE int
#define GT(A, B) (*(A) *(B))
#define MOV(A, B) (*(A) = *(B))
typedef E_TYPE e_type;

void myInsertionSort(e_type a[], size_t size);

That makes it much easier
to analyze the function algorithmically.
I rewrote myInsertionSort that way.
I found that it was better to declare (max) as a pointer
rather than as an element type.
I also added some error handling
and made some small personal stylistic changes.

/* BEGIN myInsertionSort.c */

#include <stdio.h>
#include <stdlib.h>

#define E_TYPE int
#define GT(A, B) (*(A) *(B))
#define MOV(A, B) (*(A) = *(B))

#define ARRAY_LENTH(array) (sizeof (array) / sizeof*(array))

typedef E_TYPE e_type;

void myInsertionSort(e_type a[], size_t size)
{
e_type *max;
e_type *tmp;
size_t *next;
size_t i,j;
size_t first = 0;
size_t last = 0;

next = malloc(size * sizeof *next);
tmp = malloc(size * sizeof *tmp);
if (size != 0 && (next == NULL || tmp == NULL)) {
free(next);
free(tmp);
puts("(next == NULL || tmp == NULL)");
exit(EXIT_FAILURE);
}
max = a;
for (i = 1; size i; ++i) {
if (GT(max, a + i)) {
if (GT(a + i, a + first)) {
j = first;
while (GT(a + i, a + next[j])) {
j = next[j];
}
next[i] = next[j];
next[j] = i;
} else {
next[i] = first;
first = i;
}
} else {
max = a + i;
next[last] = i;
last = i;
}
}
j = first;
for (i = 0; size != i; ++i) {
MOV(tmp + i, a + j);
j = next[j];
}
for (i = 0; size != i; ++i) {
MOV(a + i, tmp + i);
}
free(next);
free(tmp);
}

int main(void)
{
size_t i;
e_type a[] = {
3, 9, 6, 4, 1, 5, 7, 2,
10, 8, 87, 95, 456, 127,
54, 784, 65, 313, 75
};

myInsertionSort(a, ARRAY_LENTH(a));
for (i = 0; ARRAY_LENTH(a) != i; ++i) {
printf("%d, ", a[i]);
}
putchar('\n');
return 0;
}

/* END myInsertionSort.c */
--
pete
Jul 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1258
by: Lupe | last post by:
hi, I'm trying to have a kind of multi-dimensional list which seems to me to be the best way to have an array of not known extension, at the begining of the program. if I have: element ...
2
4945
by: Kushal | last post by:
Hello, I am trying to build a dynamic set/list of data which should be sorted as each element is inserted. The main criteria in this list is the speed, the time it takes to insert a element,...
5
6036
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
3
2075
by: chai | last post by:
I am trying out a program to insert an element to a sorted list(singly linked list)without using the temporary variable.Is there a solution for this problem?
4
4484
by: Raider | last post by:
Is there std::map member-function that do as code below? typedef std::map<NameClass, ValueClass> ParameterContainer; .... // this code is equivalent to "_Parameters = Value", // but a bit...
7
2048
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor...
0
4062
Ganon11
by: Ganon11 | last post by:
So far, the Articles sections are filled with slow sorting algorithms. Bubble Sort and Selection Sort, while very easy to comprehend, are relatively slow algorithms at Θ(n^2) running time. Here, I...
1
1729
by: AhmedGY | last post by:
Hi, am trying to build an app that uses the insertion sort method to sort numbers entered in a textbox and display them sorted in a label, so i wrote this inside the sort button click event: ...
9
2054
by: python_newbie | last post by:
I don't know this list is the right place for newbie questions. I try to implement insertion sort in pyhton. At first code there is no problem. But the second one ( i code it in the same pattern i...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.