Algorithm of sort the given array remove the repeated value, Programming Languages

Short Arrays:


Array = randi(10,1,randi(6,1,1)+4)-5

This command will generate an array which you should be able to evaluate by hand (and also have your code evaluate for testing purposes).

Long Arrays:

Array = randi(200,1,randi(500,1,1)+50)-100

This command will generate an array which will be too large to evaluate by hand, but which your code should still work with.

Sort the given array 

Remove the repeated values (as asked for extra credit)

Intialize solution vector

Find the last negative element in shorted array , assume its index be last_n

Outer loop

  From 1st element up to last negative element

       First atom  = array(I,i)

          Inner loop

              From last element to first positive element in array

                     Second atom = array (1,j)

                     Find the third element

                            Third element = 0- (first atom + second atom)

                            Check (third_elemnt <= second atom) % this step is to avoid repetition of same solution

                                           Search the element inarray

                                            If it is present add this to solution vector

                     Else proceed for next iteration

            Close inner loop

Close outer loop

Advantage now there are no repeated results , the number of loops has been reduced using the algorithm.

Example

Input array   = [5    -3     5     2    -4    -2     1     5     5  0]

sorted array =   -4    -3    -2     0     1     2     5     5     5     5 

Unique array and sorted = -4    -3    -2     0     1     2     5

Index of last negative number =3

Outer loop  i=1:1:3

Inner loop  j =7:-1:5

Output

sol =

 

    -4     2     2

    -3     5    -2

    -3     2     1

    -2     5    -3

    -2     2     0

    -2     1     1

There are no repeated solution

Each vector  is unique.

Posted Date: 7/23/2012 2:52:54 AM | Location : United States







Related Discussions:- Algorithm of sort the given array remove the repeated value, Assignment Help, Ask Question on Algorithm of sort the given array remove the repeated value, Get Answer, Expert's Help, Algorithm of sort the given array remove the repeated value Discussions

Write discussion on Algorithm of sort the given array remove the repeated value
Your posts are moderated
Related Questions
Write an assembly program that computes Fibonacci numbers 2-5. a. Assume Fib(0)=0 and Fib(1)=1. b. Store Fib(2) in EAX, Fib(3) in EBX, Fib(4) in ECX and Fib(5) in EDX. c. Use a LOO

Program for Operate the Rolodex Rolodex is a rotating file, usually used to record business contact information. Cards are inserted in alphabetic order by name. You operate th

The purpose of the HB Programming test is to assess whether a candidate has sufficient knowledge to join our team here and beyond that where they would fit within our teams. The

I need help programming an arduino uno to scan an ean-8 student barcode and display their name and id on computer. This is a capstone project.

Introduction A new online system for purchasing carsis being designed and you have been contracted to write a C# console application program that models the proposed functio

Objective: In a programming language of your choice, write an intelligent agent able to play block Dominoes against a player. Together with this assignment you will find two pa

W.A.S.S to find the maximum of three numbers Program # W.A.S.S to find the maximum of three numbers echo "Enter three No.:" read no1 read no2 read no3 if test $no1 -gt $n

I need to finish my game project within two weeks using XNA. Would you guys can help me to finish it? Thank you

Write a program to use a robot to move the stack of blocks from a source location to a target location, based. Movement of robot arms, picking and dropping, can be controlled usi

1. You are working as a designer for a university that offers a program in Computer Science. One of the tracts is computer security. One of your colleagues has recommended adding a