![]() |
![]() |
#1 (permalink) | |
Banned
Location: 'bout 2 feet from my iMac
|
deleting elements in an array...
Ok, I'm working on my algorithms homework, and I come across this problem:
Quote:
|
|
![]() |
![]() |
#2 (permalink) |
WARNING: FLAMMABLE
Location: Ask Acetylene
|
You mean a regular array, as in the C++ declaration
Code:
int x[100]; You don't normally delete elements from an array. I don't know of any data structure that supports deletion in O(1) time. I mean a hash table can do that if the set of keys is the same size as the potential set of elements. But that isn't much different then deleting from a regular array.
__________________
"It better be funny" ![]() Last edited by kel; 01-22-2004 at 07:34 PM.. |
![]() |
![]() |
#4 (permalink) |
beauty in the breakdown
Location: Chapel Hill, NC
|
Ha, there was a CS major explaining this to someone in the library this afternoon. I didnt join in because I dont know much about programming, but IIRC, she said just what you did--you cant really delete them, you have to make a new array minus the element you wish to delete, or, if you can, just set the element equal to null.
__________________
"Good people do not need laws to tell them to act responsibly, while bad people will find a way around the laws." --Plato |
![]() |
![]() |
#5 (permalink) |
WARNING: FLAMMABLE
Location: Ask Acetylene
|
Creating a new array and copying is an O(n) operation so that can't be it. Arrays don't have null values, unless they are arrays of pointers, which isn't specified.
Question = nonsensical.
__________________
"It better be funny" ![]() |
![]() |
![]() |
#6 (permalink) |
Upright
Location: MN, USA
|
Well, I agree that the question doesn't seem to make a lot of sense.
The closest thing I can come to that's a "real" answer (i.e., not just 'set the array element to null') is this: for(j = i; j < array.length - 1; j++) { array[j] = array[j + 1]; } array[array.length] = null; Although in a worst-case analysis this is in fact O(n), it doesn't depend directly on the length of the array in the same way that copying all the elements does. Be sure to let us know what the answer is! |
![]() |
![]() |
#7 (permalink) |
The GrandDaddy of them all!
Location: Austin, TX
|
i'm thinking hash tables here.
the actual table being a linked list which keeps track of the previous and the next elements. if the hash table is organized by element #, then u should be able to go into x element and remove it. same thing with the b part.
__________________
"Luck is what happens when preparation meets opportunity." - Darrel K Royal |
![]() |
![]() |
#9 (permalink) | |
Wehret Den Anfängen!
Location: Ontario, Canada
|
Code:
void delete_ith(element_type* array, int* array_length, int i) { array[i] = array[*array_length-1]; *array_length = *array_length-1; } The actual memory allocated to the array might be the same, but 1 less element is used, and the ith element has been deleted/overwritten. Quote:
1> it can't be done. 2> use markers for "unused value". 3> Implement a hash table instead of an array. Takes practical constant time, theoretical (when you pay attention to the dots on the i's) lg n time. 4> Implement a skip list instead of an array. Takes probabalistic-average lg n time.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest. |
|
![]() |
![]() |
#10 (permalink) | |
42, baby!
Location: The Netherlands
|
Quote:
a) just set array[i ] to zero/null. b) use linked lists (pointers). simply drop item i, have item [i-1] point to item [i+1]. To solve both problems at once seems impossible. With a normal array, problem b seems impossible, because sorting an array takes n steps at least. With linked lists, problem a seems impossible, because deleting item i is dependent on the size of n (you have to search for the item). I got the big olde book of data structures, which tells me that no matter which data structure you use, either a or b depends on the array size n in some way. The most efficient option I saw was a heap (https://users.cs.jmu.edu/bernstdh/we...troduction.php). |
|
![]() |
![]() |
#12 (permalink) |
Junkie
Location: San Francisco
|
Okay I just rewrote the answer to part (a) then realized that was exactly what Yakk already said... Heh. The null thing is even worse because then you need to rewrite all your code to check for nulls whenever you iterate or dereference an array (and you WILL forget that little nested for loop that causes your program to randomly crash and burn), and you can't have a valid value equal to null in the array.
For the sorted array it's a little trickier because you need to move things around so it's still dependent on the size of the array. I don't believe it's technically possible to do that for any given sorted array without resorting to even worse hackery which kind of defeats the whole purpose of using one. If you really need to do deletions in O(1) time maybe you are using the wrong data structure. ![]() A hash table supports deletion in O(1) time, but by most definitions a hash table is not an array. There are exceptions such as in PHP where an 'array' is actually implemented as a hash table, but a PHP array is quite different from a traditional low-level array.
__________________
"Prohibition will work great injury to the cause of temperance. It is a species of intemperance within itself, for it goes beyond the bounds of reason in that it attempts to control a man's appetite by legislation, and makes a crime out of things that are not crimes. A Prohibition law strikes a blow at the very principles upon which our government was founded." --Abraham Lincoln Last edited by n0nsensical; 02-21-2009 at 06:31 PM.. Reason: i am all fucked up! |
![]() |
![]() |
#13 (permalink) |
Young Crumudgeon
Location: Canada
|
Apparently. For a banned member, no less.
__________________
I wake up in the morning more tired than before I slept I get through cryin' and I'm sadder than before I wept I get through thinkin' now, and the thoughts have left my head I get through speakin' and I can't remember, not a word that I said - Ben Harper, Show Me A Little Shame |
![]() |
![]() |
#15 (permalink) |
Junkie
Location: San Francisco
|
I remember when she got banned, I couldn't figure it out, hell I still don't know.
__________________
"Prohibition will work great injury to the cause of temperance. It is a species of intemperance within itself, for it goes beyond the bounds of reason in that it attempts to control a man's appetite by legislation, and makes a crime out of things that are not crimes. A Prohibition law strikes a blow at the very principles upon which our government was founded." --Abraham Lincoln |
![]() |
Tags |
array, deleting, elements |
|
|