Download presentation
Presentation is loading. Please wait.
Published byIris McBride Modified over 8 years ago
1
Arrays, Link Lists, and Recursion Chapter 3
2
Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts one element at a time. The algorithm takes an element from the list and places it in the correct location in the list. This process is repeated until there are no more unsorted items in the list. Computational complexity for insertion sort is O(n 2 ),making it less efficient than more advanced sorting algorithms, such as quick sort, heap sort, or merge sort, especially for large lists.
3
High level description of the insertion sort algo.
4
Details of Insertion Sort Intermediate-level description of the insertion-sort algorithm
5
Java code for performing insertion-sort on an array of characters
7
Java.util.Array Methods We list below some simple methods of class java.util.Arrays that need no further explanation: equals(A, B): Returns true if and only if the array A and the array B are equal. Two arrays are considered equal if they have the same number of elements and every corresponding pair of elements in the two arrays are equal. That is, A and B have the same elements in the same order. fill(A,x): Stores element x into every cell of array A. sort (A): Sorts the array A based on natural ordering of its elements, which must be comparable.This methods uses the quick –sort algorithm discussed in chapter 11. CopyOf(A,n): returns an array of size n such that the first k elements of this array are copied from A, where k = min {n,A.length}. If n>A.length then the last n-A.length elements in this array will be padded with defaults values. toString(A): Returns a String representation of the array A. For example, the following string would be returned by the method toString called on an array of integers A = [4,5,2,3,5,7,10]: [4, 5, 2, 3, 5, 7, 10]
8
Psedu-Random Number Generation Java has a built-in class java.util.Random, whose instance are pseduo-random number generators. Objects compute a sequence of numbers that are statically random. Its possible to predict the next generated number in the sequence given the past list numbers: next = (a * cur +b ) % n; Use setSeed() method to determine the place to start generating the first number to base on (seed). Common trick to generate a different sequence each time a program is run is to use a seed that will be different for each run. We can use the current time in milliseconds as a seed.
9
Sample Run: arrays equal before sort: true arrays equal after sort: false old = [41,38,48,12,28,46,33,19,10,58] num = [10,12,19,28,33,38,41,46,48,58]
10
Single Link List Introduction Introduction Representation Representation ◦ Space Analysis Creation and Insertion Creation and Insertion Deletion Deletion
11
Introduction Storing data items in arrays has at least two limitations ◦ The array size is fixed once it is created: Changing the size of the array requires creating a new array ad then copying all data from the old array to the new array ◦ The data items in the array are next to each other in memory: Inserting an item inside the array requires shifting other items A linked structure is introduced to overcome limitations of arrays and allow easy insertion and deletion ◦ A collection of nodes storing data items and links to other nodes ◦ If each node has a data field and a reference field to another node called next or successor, the sequence of nodes is referred to as a singly linked list ◦ Nodes can be located anywhere in the memory ◦ The first node is called head and the last node is called tail
12
Representation We are using a representation in which a linked list has both head and tail references.
13
Partial implementation of the class for a singly linked list / ** Singly linked list **/ public class SLinkedList { protected Node head; // head node of the list protected Node tail; // tail node of the list protected long size; // number of nodes in the list /** Default constructor that creates an empty list **/ public SLinkedList () { head = tail = null; size =0; } // update and search methods would go here …. }
14
Representation: Space Analysis Now, we can take a look at the space requirements: ExplanationSpace Require The list reference has three fields: head (type: Node), tail (type: Node) and size (type long) = 2 sizeof(Node) + sizeof (long) sizeof(SLinkedList) The list has N elements of type Node. Each element has two fields– element (type String) and next (type Node). N sizeof(Node)
15
Inserting in a Singly Linked List 1. Insert from the beginning of the list We can easily insert elements at the head of the list: I. Create a new node II. Set its next link to refer to the same object as head III. Set the head to point to the new node
16
Inserting a new node v at the beginning of a singly linked list. Note that this method works even if the list is empty. Note that we set the next pointer for the new node v before we make variable head point to v.
17
Inserting in a Singly Linked List 2. Insert from the tail of the list We can easily insert elements at the head of the list: I. Create a new node II. Set its next link to point to null III. Set the next reference of the tail to point to the new object IV. Set the tail to point to the new node
18
Inserting a new node at the end of a singly linked list. This method works also if the list is empty. Note that we set the next pointer for the old tail node before we make variable tail point to the new node.
19
Removing an Element in a Singly Linked List
20
Doubly Linked Lists Representation ◦ Space Analysis Creation and Insertion Deletion
21
Representation Doubly linked list : is a type of linked list that allows us to go in both directions – forward and reverse – in a linked list. A node in a doubly linked list stores two references—a next link which points to the next node in the list, and a prev link, which points to the previous node in the list. Motivation for Doubly link list ;provides a flexible way to traversal the list in both directions so we can perform insertion and deletion from both sides.
22
Doubly Linked Lists : Space Analysis The space requirements of our representation of the doubly linked lists is as follows: ExplanationSpace Require The list reference has three fields: head (type: Node), tail (type: Node) and size (type long) = 2 sizeof(Node) + sizeof (long) sizeof(DLinkedList) The list has N elements of type Node. Each element has two fields– element (type String) and next (type Node) prev ( Type Node) N sizeof(Node)
23
Header and Trailer Sentinels Header and trailer are a “dummy” or sentinel nodes. An empty list would have these sentinels pointing to each other Main Operations on Doubly linked List 1.Remove the last Node
25
2. Add a node to the front
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.