C++ Searching, Sorting, and Pointers

Ace your homework & exams now with Quizwiz!

How do you use pointer arithmetic?

*(arrayName + 2);

How does selection sort work?

1. Finds the smallest element in the list and swaps it with the element in the first position. 2. Then it finds the second smallest element and swaps it with the element in the second position. 3. It does this until we reach the last position.

How does bubble sort work?

1. Going left to right, you compare consecutive elements. 2. Whenever two are out of place, you swap them. It keeps going until the largest element is in the last place. It keeps going until there are no swaps

What is a constant pointer?

A pointer whose address cannot change

What is a pointer variable?

A variable that holds an address and allow you to access memory locations

Where does the array name start?

An array name starts at the first element of an array

What is a Basic Step?

An operation in the algorithm that executes in a constant amount of time

How does a linear search work?

It compares the value to each number in an array until it finds the number or until it reaches the end of the array

What is the indirection operator and what does it do?

It is an asterisk and allows you to access the value of a pointer

What are the advantages and disadvantages of bubble sort?

It is easy to understand and implement but it's inefficient and slow for large arrays

What are the advantages and disadvantages of selection sort?

It is more efficient than bubble sort, but some consider it to be harder to understand

How does a binary search work?

It looks at the number in the middle. If the number is higher than the value, we eliminate all the lower values and look only at the higher values. If the number is lower, we eliminate all the higher values and only look at the lower values.

Is bounds checking performed on array access?

No

What is the runtime complexity of bubble sort?

O(N^2)

What is the runtime complexity of selection sort?

O(N^2)

What is a Computation Problem?

Problem solved by an algorithm

What is the efficiency of an algorithm measured by?

Space (computer memory used) Time (how long to execute the algorithm)

What is a disadvantage of binary search?

The array elements must be sorted

When does a binary search end?

The binary search repeats until it finds the value or the low, mid, and high are all the same

How do you find the mid value in a binary search?

You add the low position and the high position and divide by two

How do you print the value of a pointer?

cout << *ptr; //shows the value

How do I display the address of an array?

cout << arrayName; //displays the address

How do I display the value in an array?

cout << array[0]; //shows the first element or cout << *arrayName;

How do you print the address of a pointer

cout << ptr; //shows the address

How do you define a constant pointer?

int * const ptrName = &size;

How do you define a pointer?

int *ptr

How do you assign a pointer?

ptr = &num;


Related study sets

Carman Essentials of Pediatric Nursing 3rd Ed - Ch. 21 Nursing Care of the Child With an Alteration in Urinary Elimination/Genitourinary Disorder

View Set

Building Classroom Environment Ch. 5-11

View Set

The Spiritual Test Summative Test

View Set

Macro Springtest3 prep fed funds

View Set

7 fundamentals of reconnaissance

View Set

3118 Pharmacology Final - EXAM 2

View Set

The Tempest: Act 3, Scene TWO -- p3

View Set

AP Psychology: Chapter 1 - Case Study, Survey, and Natural Observation Methods

View Set

Small business management unit 3

View Set

Textbook Chapter 2 - Biology - Chemistry of Life

View Set