473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Algorithm question

AMP
Hello,
I just started working with Introduction to Algoriths from MIT Press
and my goal is to rewrite them in c#. This is just a learning
experiance.
The following code:

INSERTION-SORT(A)
for j ← 2 to length[A] // 1 Based Array
{
do key ← A[j]
i ← j - 1
while i 0 and A[i] key
{
do A[i + 1] ← A[i]
i ← i - 1
}
A[i + 1] ← key
}

Becomes:
for (int j = 1; j <= (length-1); j++) // 0 based array
{
uint CurrentCard=IntCard[j];
int i =j-1;
while ((i 0) & (IntCard[i] CurrentCard))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;
}

IntCard[i+1]=CurrentCard;
After much head banging I realized the original Algorithm would also
fail in a 1 based array because at i=i-1 becomes 0 and there is no 0 in
the 1 based array.

My question is:
How do I check if the element exists before I run the while loop AND is
this the best way of doing this.Dont forget I dont want to use any
premade functions,I just want a simple algorithm.

Thanks
Mike

Nov 29 '06 #1
1 1931
AMP wrote:
Hello,
I just started working with Introduction to Algoriths from MIT Press
and my goal is to rewrite them in c#. This is just a learning
experiance.
The following code:

INSERTION-SORT(A)
for j ← 2 to length[A] // 1 Based Array
{
do key ← A[j]
i ← j - 1
while i 0 and A[i] key
{
do A[i + 1] ← A[i]
i ← i - 1
}
A[i + 1] ← key
}

Becomes:
for (int j = 1; j <= (length-1); j++) // 0 based array
{
uint CurrentCard=IntCard[j];
int i =j-1;
while ((i 0) & (IntCard[i] CurrentCard))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;
}

IntCard[i+1]=CurrentCard;
After much head banging I realized the original Algorithm would also
fail in a 1 based array because at i=i-1 becomes 0 and there is no 0 in
the 1 based array.
When i = 0, the code will fall out of the loop and will not try to
process IntCard[0]. Incidentally in your while loop you should be
using && and not &. & is a bitwise operator. && is the logical and
operator.
>
My question is:
How do I check if the element exists before I run the while loop AND is
this the best way of doing this.Dont forget I dont want to use any
premade functions,I just want a simple algorithm.
In C# when you declare an array of ints, each element is defaulted to 0
so unless you make 0 indicate an "empty" element, I know of no way to
do this. For reference types, you can check them for null.

Chris

Nov 29 '06 #2

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

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: junky_fellow | last post by:
How do we calculate the complexity of an algorithm? Am i right if i say the complexity of an algorithm is the number of comparisons done in that algorithm? thanx in advance .......
12
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
1
by: vermarajeev | last post by:
Hi, EveryBody This question is really interesting one. My question is related to "STL Find Algorithm" Defination Direct from book Now my questions are
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.