1 / 10

Arrays

Arrays. 2011, Fall Pusan National University Ki-Joune Li. a k , a k +1. Most Basic Data Structure. Primitive Elements : Integer, Float, String, etc.. Container of elements Ordered or not Duplicated or Not How to define the same object

aldis
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays 2011, Fall Pusan National University Ki-Joune Li

  2. ak, ak+1 Most Basic Data Structure • Primitive Elements : Integer, Float, String, etc.. • Container of elements • Ordered or not • Duplicated or Not • How to define the same object • In fact, any container of elements is stored in ordered way. • Only the interface makes it different

  3. Operations • Operation defines the interface (or nature) to the users • Should be implemented, whatever the internal implementation • Operations • Unordered • Maintenance : create a new container, Insert, Delete, Update (?) • Search : search by atttributes • Information or statistics : number of elements, max, min, etc. • Ordered • Operations for Unordered Container + • Scan : get the k-th, the last, or the next elements • Sorting • Ordered container can be used as an unordered container

  4. Arrays • Array • Contains elements • Ordered (or unordered) • Set (No Order) • Example : Polynomial • Representation 1: float Coef[MaxDegree+1]; • Representation 2: int degree; float *Coef; Coef=new float[degree+1];

  5. 3.0 2.0 1.4 0.4 9.6 1.0 -2.4 2 101 0 1 15 0 2 Class Term { friend Polynomial; private: float coef; float exp;}; free a.Start a.Finish b.Start b.Finish MaxTerms Representation of Array • Representation 3: • Array of (Coefficient, Exponent): ((am,m),(am-1,m), … (a0,0)) • Sparse Array : Example. • 3.0x101-2.4x2+1.0x+9.6 : ((3.0,101),(-2.4,2),(1.0,1),(9.6,0)) • MaxTerms Class Polynomial { private: static Term termArray[MaxTerms]; static int free; int Start, Finish;};

  6. 3.0 -2.4 1.0 9.6 101 2 1 0 Example : Adding two polynomials A = 3.0x101 - 2.4x2 + 1.0x + 9.6 4.0 15 1.4 2 0.4 0 B = 4.0x15 + 1.4x2 + 0.4 C = 3.0 101 4.0 15 -1.0 2 1.0 1 10.0 0 Termination Condition - Aptr > A.finish or Bptr > B.finish If Terminated by Aptr > A.finish Append the rest of B to the tail of B Time Complexity : O(LenA + LenB )

  7. Sparse Matrix • Sparse Matrix • Matrix with many zero elements • Two Representations Class MatrixTerm { friend SparseMatrix; private: int row,col; int value;}; vs. But Row Major ! Class SparseMatrix { private: int nRows,nCols,nElements; MatrixElements smArray[MaxElements];};

  8. Transposing a Matrix A A.nCols= 6, A.nRows=7A.nElements=5 AT Algorithm MatrixTranspose(SparseMatrix A) SparseMatrix B; swap(A.nRows,A.nCols); countB=0; if(A.nElements>0) { // for non-empty matrix for(c=0;c<A.nCols;c++) // for each element c of A for(i=0;i<A.nElements;i++) { // find elements in column c if(A.smArrays[i].col==c) { B.smArray[countB].row=c; B.smArray[countB].col=A.smArray[i].row; B.smArray[countB].value=A.smArrary[countB].value; ++countB; } } } return B;end Algorithm Time Complexity : O(nCols·nElements) = O(nCols2·nRows) > O(nCols·nRows)

  9. A’ row col value Sort by (row, col) 1 1 1 0 4 4 1 4 2 Exchange row  col 5 5 1 2 6 6 AT row col value 0 4 4 1 1 1 1 4 2 2 6 6 5 5 1 Transposing a Matrix: An Improved Algorithm A Time Complexity : O(nElements+ nElements log nElements) = O(nElements log nElements)

  10. AT index row col value 0 1 3 5 1 0 0 2 2 1 3 3 5 0 4 0 3 1 22.5 4.5 82.5 1.5 3.5 12.5 22.5 2.5 2 3 4 5 6 7 0 1 2 3 4 5 0 1 2 3 4 5 2 2 2 1 0 1 0 2 4 6 7 7 AT Row Size ATRow Start Transposing a Matrix: Another Improved Algorithm A Time Complexity : O(ncol+ ncol + nElements)

More Related