Skip to content

This project focuses on optimizing the search functionality for a large dataset, a JSON file containing over 200,000 city entities. The key optimization technique implemented is binary search, which significantly enhances search performance.

Notifications You must be signed in to change notification settings

calm-days/City-Search-binary-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 

Repository files navigation

City Search

Project Overview

This project focuses on optimizing the search functionality for a large dataset, a JSON file containing over 200,000 city entities. The key optimization technique implemented is binary search, which significantly enhances search performance.

city_search

View Controllers

The project consists of two primary view controllers: CitiesViewController and MapViewController.

CitiesViewController

The CitiesViewController incorporates a SearchController for carrying out searches and a TableView for displaying search results. It utilizes a segue to transition to the MapViewController in order to display the selected city's map. The data for this controller is sourced from CitiesController.

MapViewController

A standard ViewController that features an MKMapView. The city information is passed as a parameter from CitiesViewController. In this view, a pin is displayed on the map to represent the selected city's location.

Data Controllers

The project contains one data controller, named CitiesController.

CitiesController

CitiesController employs a shared instance due to the need for initialization.

Originally, the intention was for the controller to return an array of results to CitiesViewController. However, considering the performance optimization requirements for handling over 200,000 items, all cities are maintained in the cityList array, while the range of search results is stored in searchResultsIndex.

Initially, filtering the cityList array was slow. To enhance performance, the code was modified to keep all cities in the cityList array and utilize searchResultsIndex for maintaining the indexes of the first and last valid search elements. This improved performance.

Implementing Binary Search

Searching items sequentially is slow and inefficient, especially since the cityList array is already sorted. Consequently, binary search was implemented, resulting in a performance boost. This improvement significantly enhances the user experience by displaying results instantly as the user types.

Data models

Two data models are included in the project: City and Coordinates:

  • City is a Codable struct containing the essential fields for the project. It also has the formattedString property, which returns the required format for table cells, and searchValue, a lowercased version of formattedString. Since it is read multiple times during sorting and slowed down the app's loading time, its value is set by CitiesController.parseSearchValues() after loading all cities from the JSON file.
  • Coordinates is a Codable struct containing a city's latitude and longitude. The City class includes a coordinate property that uses the lat and lon values to return a proper CLLocationCoordinate2D object.

Unit Tests

The project features tests for the CitiesController class, including searches that cover valid results and those that return no results.

About

This project focuses on optimizing the search functionality for a large dataset, a JSON file containing over 200,000 city entities. The key optimization technique implemented is binary search, which significantly enhances search performance.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages