# Arrays 2008, Fall Pusan National University Ki-Joune Li.

## Presentation on theme: "Arrays 2008, Fall Pusan National University Ki-Joune Li."— Presentation transcript:

Arrays 2008, Fall Pusan National University Ki-Joune Li

STEMPNU 2 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 a k, a k+1

STEMPNU 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

STEMPNU 4 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]; Arrays

STEMPNU 5 Representation of Array Representation 3: Array of (Coefficient, Exponent): ((a m,m),(a m-1,m), … (a 0,0)) Sparse Array : Example. 3.0x 101 -2.4x 2 +1.0x+9.6 : ((3.0,101),(-2.4,2),(1.0,1),(9.6,0)) MaxTerms How to define the end ? 2.0151.420.40 MaxTerms Class Polynomial { private: static Term termArray[MaxTerms]; static int free; int Start, Finish; }; Class Term { friend Polynomial; private: float coef; float exp; }; 3.0101-2.421.019.60 a.Starta.Finish b.Startb.Finish free

STEMPNU 6 Example : Adding two polynomials 3.0101-2.421.019.60 A = 3.0x 101 - 2.4x 2 + 1.0x + 9.6 B = 4.0x 15 + 1.4x 2 + 0.4 4.0151.420.40 3.01014.01521.0110.00 C = 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(Len A + Len B )

STEMPNU 7 Sparse Matrix Matrix with many zero elements Two Representations 1234567 10000000 20100000 30000000 40000000 54000200 60000010 70060000 RowColValue 221 514 552 661 736 vs. Class MatrixTerm { friend SparseMatrix; private: int row,col; int value; }; Class SparseMatrix { private: int nRows,nCols,nElements; MatrixElements smArray[MaxElements]; }; But Row Major !

STEMPNU 8 Transposing a Matrix row colvalue 111 404 412 551 626 row colvalue 044 111 142 266 551 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 O(nCols·nRows) A A ATAT ATAT A.nCols= 6, A.nRows=7 A.nElements=5

STEMPNU 9 Transposing a Matrix: An Improved Algorithm row colvalue 111 404 412 551 626 A A 155 662 241 111 440 col row ATAT ATAT 662 155 241 440 111 value col row A A Exchange row col Sort by (row, col) Time Complexity : O(n Elements + n Elements log n Elements ) = O(n Elements log n Elements )

STEMPNU 10 Transposing a Matrix: Another Improved Algorithm indexrowcolvalue 0004.5 1011.5 2112.5 3303.5 43212.5 53382.5 64522.5 75262.5 A A 7 6 5 4 3 2 1 0 indexvaluecolrow ATAT ATAT 1 5 01222 43210 A T Row Size 7 5 76420 43210 A T Row Start 101.5 004.5 112.5 033.5 2312.5 3382.5 5422.5 25 Time Complexity : O(n col + n col + n Elements )